3

如果我有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 简单、优雅的解决方案

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

3 回答 3

3

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

Object.getOwnPropertyNames('baz'.constructor.prototype)
于 2012-04-25T09:01:10.913 回答
1

使用可以使用以下:

$(document).ready(function () {
    var foo = 'bar';
    var div = '';
    var ob = Object.getOwnPropertyNames(foo.constructor.prototype);
    for (var i = 0; i < ob.length; i++) {
        div += ob[i] + "<br/>";
    }
    $(selector).html(div);
});
于 2012-04-25T08:58:23.483 回答
-2

使用日食:)

或者只是看这里:https ://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String

于 2012-04-25T08:43:45.807 回答