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.
我发现,有时我有一个带有很多方法和属性的var,我似乎无法找到它们,带有对象的事件。键和对象。在var和原型上都有getOwnPropertyNames。
Here's an example: I'm playing with RethinkDB and I want to override the run
function. 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:
这里有一个例子:我在玩RethinkDB,我想重写run函数。但是,我不知道它住在哪里——我需要改变什么对象原型,等等。事实上,我找不到任何方法来找到它和我上面指定的函数:
> 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?
运行函数永远不会出现……什么好主意吗?
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.
run返回一个函数——我想要包装的函数。
@T.J. Crowder's answer: findProps('run', r.eq())
prints out a bunch of stuff including:
@T.J。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!
所以这就是它!
1 个解决方案
#1
2
Object.keys
gives you that object's enumerable property names. Many properties are not enumerable.
对象。键提供对象的可枚举属性名。许多属性是不可枚举的。
As ssube said, you don't have to know at what level a property is defined to override it. 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.
正如ssube所说,您不需要知道在什么级别定义一个属性来覆盖它。但是如果你想知道,你可以在ES5中或者以后,通过Object。getOwnPropertyNames,包括对象的非可枚举属性和对象。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>
#1
2
Object.keys
gives you that object's enumerable property names. Many properties are not enumerable.
对象。键提供对象的可枚举属性名。许多属性是不可枚举的。
As ssube said, you don't have to know at what level a property is defined to override it. 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.
正如ssube所说,您不需要知道在什么级别定义一个属性来覆盖它。但是如果你想知道,你可以在ES5中或者以后,通过Object。getOwnPropertyNames,包括对象的非可枚举属性和对象。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>