本文整理匯總了Golang中github.com/robertkrimen/otto.Object.Call方法的典型用法代碼示例。如果您正苦於以下問題:Golang Object.Call方法的具體用法?Golang Object.Call怎麽用?Golang Object.Call使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/robertkrimen/otto.Object
的用法示例。
在下文中一共展示了Object.Call方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: printObject
func (ctx ppctx) printObject(obj *otto.Object, level int, inArray bool) {
switch obj.Class() {
case "Array":
lv, _ := obj.Get("length")
len, _ := lv.ToInteger()
if len == 0 {
fmt.Printf("[]")
return
}
if level > maxPrettyPrintLevel {
fmt.Print("[...]")
return
}
fmt.Print("[")
for i := int64(0); i < len; i++ {
el, err := obj.Get(strconv.FormatInt(i, 10))
if err == nil {
ctx.printValue(el, level+1, true)
}
if i < len-1 {
fmt.Printf(", ")
}
}
fmt.Print("]")
case "Object":
// Print values from bignumber.js as regular numbers.
if ctx.isBigNumber(obj) {
numberColor.Print(toString(obj))
return
}
// Otherwise, print all fields indented, but stop if we're too deep.
keys := ctx.fields(obj)
if len(keys) == 0 {
fmt.Print("{}")
return
}
if level > maxPrettyPrintLevel {
fmt.Print("{...}")
return
}
fmt.Println("{")
for i, k := range keys {
v, _ := obj.Get(k)
fmt.Printf("%s%s: ", ctx.indent(level+1), k)
ctx.printValue(v, level+1, false)
if i < len(keys)-1 {
fmt.Printf(",")
}
fmt.Println()
}
if inArray {
level--
}
fmt.Printf("%s}", ctx.indent(level))
case "Function":
// Use toString() to display the argument list if possible.
if robj, err := obj.Call("toString"); err != nil {
functionColor.Print("function()")
} else {
desc := strings.Trim(strings.Split(robj.String(), "{")[0], " \t\n")
desc = strings.Replace(desc, " (", "(", 1)
functionColor.Print(desc)
}
case "RegExp":
stringColor.Print(toString(obj))
default:
if v, _ := obj.Get("toString"); v.IsFunction() && level <= maxPrettyPrintLevel {
s, _ := obj.Call("toString")
fmt.Printf("<%s %s>", obj.Class(), s.String())
} else {
fmt.Printf("<%s>", obj.Class())
}
}
}
示例2: toString
func toString(obj *otto.Object) string {
s, _ := obj.Call("toString")
return s.String()
}