[英]How to find all Javascript object methods and properties?
I've found that sometimes I'll have a var with a bunch of methods and properties that I can't seem to locate somehow, event with Object.keys
and Object.getOwnPropertyNames
on both the var and the prototype. 我发现有时候我会有一个带有一堆我似乎无法找到的方法和属性的
Object.getOwnPropertyNames
,在var和原型上都带有Object.keys
和Object.getOwnPropertyNames
事件。
Here's an example: I'm playing with RethinkDB and I want to override the run
function. 这是一个例子:我正在玩RethinkDB,我想重写
run
函数。 However, I don't know where it lives -- what object prototype I need to change, etc. In fact, I can't find any way of finding it with the functions I specified above: 但是,我不知道它在哪里-我需要更改什么对象原型,等等。实际上,我找不到用上面指定的函数找到它的任何方法:
> r.db('test').tableCreate('authors').run
[Function]
> r.db('test').tableCreate('authors')
{ [Function]
args:
[ { [Function] args: [Object], optargs: {} },
{ [Function] data: 'authors' } ],
optargs: {} }
> r.db('test').tableCreate('authors').prototype
{}
> r.db('test').tableCreate('authors').run
[Function]
> Object.keys(r.db('test').tableCreate('authors'))
[ 'args', 'optargs' ]
> typeof r.db('test').tableCreate('authors')
'function'
> Object.getOwnPropertyNames( r.db('test').tableCreate('authors') )
[ 'length',
'name',
'arguments',
'caller',
'prototype',
'args',
'optargs' ]
> Object.getOwnPropertyNames( r.db('test').tableCreate('authors').prototype )
[ 'constructor' ]
The run
function never shows up... Any ideas? run
功能从不显示...有什么想法吗?
EDIT: 编辑:
I did some snooping in the source code. 我在源代码中做了一些窥探。 this is the method I want to wrap .
这是我要包装的方法 。
Then, you can following the inheritance chain from TermBase to Eq ( RDBVal , RDBOp , Eq ). 然后,您可以遵循从TermBase到Eq( RDBVal , RDBOp , Eq )的继承链。
r.eq().run
returns a function -- the function I want to wrap. r.eq().run
返回一个函数-我想包装的函数。
@TJ Crowder's answer: findProps('run', r.eq())
prints out a bunch of stuff including: @TJ Crowder的答案:
findProps('run', r.eq())
打印出很多东西,包括:
I20150625-10:33:31.047(-7)? Props for run[[Proto]][[Proto]][[Proto]][[Proto]]
I20150625-10:33:31.047(-7)? 0: constructor
I20150625-10:33:31.047(-7)? 1: showRunWarning
I20150625-10:33:31.047(-7)? 2: run
So thats it! 就是这样了!
Object.keys
gives you that object's enumerable property names. Object.keys
为您提供该对象的可枚举属性名称。 Many properties are not enumerable. 许多属性是无法枚举的。
As ssube said , you don't have to know at what level a property is defined to override it. 如ssube所说 ,您不必知道在什么级别定义属性来覆盖它。 But if you want to know, you can in ES5 and later, via
Object.getOwnPropertyNames
, which includes non-enumerable properties of an object, and Object.getPrototypeOf
, which lets you traverse up the object's prototype chain. 但是,如果你想知道的,你可以在ES5后来,通过
Object.getOwnPropertyNames
,其中包括对象的非枚举的属性,以及Object.getPrototypeOf
,它可以让你穿越了对象的原型链。
Example: 例:
function findProps(objname, obj) { var p; snippet.log("Props for " + objname); Object.getOwnPropertyNames(obj).forEach(function(name, index) { snippet.log(index + ": " + name); }); p = Object.getPrototypeOf(obj); if (p != null) { findProps(objname + "[[Proto]]", p); } } var a = {}; Object.defineProperty(a, "foo", { // A non-enumerable property value: "bar" }); var b = Object.create(a); // b's prototype is a b.answer= 42; // An enumerable property Object.defineProperty(a, "question", { // A non-enumerable property value: "Life, the Universe, and Everything" }); var c = Object.create(b); // c's prototype is b c.last = "property"; findProps("c", c);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.