简体   繁体   中英

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.

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:

> 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 ).

r.eq().run returns a function -- the function I want to wrap.

@TJ Crowder's answer: findProps('run', r.eq()) prints out a bunch of stuff including:

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. 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.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM