我有以下代码:
function test() {
this.a = 5;
this.b = 6;
}
test.prototype.b = 10;
test.prototype.c = 12;
var example = new test();我要怎么找出example.something
A.在函数对象中只有一个值?
B.在原型中只有一个值?
C.在函数对象和原型中都有一个值?
发布于 2016-08-19 00:26:01
您可以使用Object.keys方法检查对象及其原型中的属性。
function test() {
this.a = 5;
this.b = 6;
}
test.prototype.b = 10;
test.prototype.c = 12;
var example = new test();
console.log(Object.keys(example));
console.log(Object.keys(example.__proto__));
发布于 2016-08-19 00:27:05
这段代码揭示了以下情况:
function test() {
this.a = 5;
this.b = 6;
}
test.prototype.b = 10;
test.prototype.c = 12;
var example = new test();
for (prop of ['a', 'b', 'c']) {
if (example.hasOwnProperty(prop)) console.log(prop + ' is owned by the object');
if (test.prototype.hasOwnProperty(prop)) console.log(prop + ' is owned by the object prototype');
}
https://stackoverflow.com/questions/39029357
复制相似问题