列出 Javascript 对象的所有原型属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30158515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:49:58  来源:igfitidea点击:

List Down All Prototype Properties of an Javascript Object

javascript

提问by Oli Soproni B.

Is there any other way to look up for the prototype properties of an javascript object. Lets say I have like this.

有没有其他方法可以查找 javascript 对象的原型属性。可以说我有这样的。

function proton() {
    this.property1 = undefined;
    this.property2 = undefined;
};

proton.prototype = {

    sample1 : function() {
        return 'something';
    },

    sample2 : function() {
        return 'something';
    }

};

var my_object = new proton();

console.log(Object.keys(my_object));

returns ["property1", "property2"]

返回 ["property1", "property2"]

console.log(Object.getOwnPropertyNames(my_object));

returns ["property1", "property2"]

返回 ["property1", "property2"]

But what i want to print is the prototype properties of the object my_object.

但我想打印的是对象 my_object 的原型属性。

['sample1', 'sample2']

['样品1','样品2']

In order for me to see the prototype properties of that object i need to console.log(object) and from developer tools i can look up for the properties of that object.

为了让我看到该对象的原型属性,我需要使用 console.log(object) 并从开发人员工具中查找该对象的属性。

But since I am using third party libraries like phaser.js, react.js, create.js so i don't know the list of the prototype properties of a created object from this libraries.

但是由于我使用的是像 phaser.js、react.js、create.js 这样的第三方库,所以我不知道从这些库中创建的对象的原型属性列表。

Is there any prototype function of Object to list down all the prototpye properties of a javascript object?

是否有 Object 的原型函数来列出 javascript 对象的所有 prototpye 属性?

回答by Bergi

Not a prototype method, but you can use Object.getPrototypeOfto traverse the prototype chain and then get the own property names of each of those objects.

不是原型方法,但您可以使用Object.getPrototypeOf它遍历原型链,然后获取每个对象的属性名称。

function logAllProperties(obj) {
     if (obj == null) return; // recursive approach
     console.log(Object.getOwnPropertyNames(obj));
     logAllProperties(Object.getPrototypeOf(obj));
}
logAllProperties(my_object);

Using this, you can also write a function that returns you an array of all the property names:

使用它,您还可以编写一个函数来返回所有属性名称的数组:

function props(obj) {
    var p = [];
    for (; obj != null; obj = Object.getPrototypeOf(obj)) {
        var op = Object.getOwnPropertyNames(obj);
        for (var i=0; i<op.length; i++)
            if (p.indexOf(op[i]) == -1)
                 p.push(op[i]);
    }
    return p;
}
console.log(props(my_object)); // ["property1", "property2", "sample1", "sample2", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable"

回答by Amadan

function prototypeProperties(obj) {
  var result = [];
  for (var prop in obj) {
    if (!obj.hasOwnProperty(prop)) {
      result.push(prop);
    }
  }
  return result;
}

EDIT: This will grab all the properties that were defined on any ancestor. If you want a more granular control of what is defined where, Bergi's suggestion is good.

编辑:这将获取在任何祖先上定义的所有属性。如果您想对定义的内容进行更精细的控制,Bergi 的建议很好。