未在 Object.keys 或 Object.getOwnPropertyNames 下列出但可以调用的函数 [英] Function not listed under Object.keys or Object.getOwnPropertyNames but can be invoked
问题描述
假设有一些库 javascript 对象 jsObj
.在调用 Object.keys
或 Object.getOwnPropertyNames
时,我得到一个属性列表,如
Suppose there is some library javascript object jsObj
. On calling Object.keys
or Object.getOwnPropertyNames
, I get a list of properties like
[a,b,c,d]
但我仍然可以调用像 jsObj.e()
这样的函数.为什么方法 e
不是 Object.keys
或 Object.getOwnPropertyNames
的一部分?他们是怎么做的?
But still I can call a function like jsObj.e()
. Why the the method e
is not part of Object.keys
or Object.getOwnPropertyNames
? How they are doing it?
这里,它说Object.getOwnPropertyNames
也将返回不可枚举的属性.那么上面的 e
属性的特征是什么?
Here, it says that Object.getOwnPropertyNames
will return non enumerable properties too. So what is the characterstic of property like e
above.
我正在使用 opentok 服务器端 SDK.使用以下代码,
I am using opentok server side SDK. Using following code,
var OpenTok = require('opentok');
var opentok = new OpenTok(config.tokbox.apiKey, config.tokbox.secret);
console.log("opentok1", Object.getOwnPropertyNames(opentok));
prints -> // [ '_client',
'apiKey',
'apiSecret',
'apiUrl',
'startArchive',
'stopArchive',
'getArchive',
'deleteArchive',
'listArchives' ]
console.log("opentok2", opentok.createSession);
prints -> function (...){...}
推荐答案
Object.e
必须在对象的原型上定义.像这样:
Object.e
must be defined on the object's prototype. Like this:
var test = function() {}
test.prototype = { e: function() { return 'e'; } }
var obj = new test();
Object.keys(obj) // returns []
obj.e() // returns 'e'
获取原型键的一种方法是简单地获取原型并使用 Object.keys()
函数:
A way of getting the prototypes keys is simply getting the prototype and the use the Object.keys()
function:
Object.keys(Object.getPrototypeOf(obj))
然而,这不会为您提供原型原型的密钥.
This will however not give you keys of the prototypes prototype.
这篇关于未在 Object.keys 或 Object.getOwnPropertyNames 下列出但可以调用的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!