gpt4 book ai didi

javascript - 如何打印 JavaScript 非对象的所有函数?

转载 作者:行者123 更新时间:2023-11-29 15:00:54 29 4
gpt4 key购买 nike

如果我有 var foo = 'bar';,我如何获得所有可在 foo 上调用的函数,例如 toUpperCase?它不是对象,所以 Object.getOwnPropertyNames(foo)不起作用:

$ node
> var foo = 'bar';
undefined
> console.log(Object.getOwnPropertyNames(foo));
TypeError: Object.getOwnPropertyNames called on non-object
at Function.getOwnPropertyNames (native)
at repl:1:20
at REPLServer.eval (repl.js:80:21)
at repl.js:190:20
at REPLServer.eval (repl.js:87:5)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)

katspaugh's simple, elegant solution :

> console.log(Object.getOwnPropertyNames(foo.constructor.prototype));
[ 'constructor',
'length',
'toLowerCase',
... ]
undefined

最佳答案

你想要的方法在String.prototype上,所有字符串的原型(prototype),Valindor的统治者。

Object.getOwnPropertyNames('baz'.constructor.prototype)

关于javascript - 如何打印 JavaScript 非对象的所有函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312126/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com