首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否显示原型对象的属性?

是否显示原型对象的属性?
EN

Stack Overflow用户
提问于 2012-11-22 03:14:53
回答 1查看 56关注 0票数 0

在这里,我有一个简单的继承函数,它接受一个对象参数,并返回一个新创建的对象,其中传递的对象作为它的原型对象。

代码语言:javascript
复制
function inherit(p) {
if (p == null) throw TypeError(); //p must not be null
if(Object.create) { //if the object.create method is defined
    return Object.create(p); //use it
}

//type checking
var typeIs = typeof p; //variable that holds type of the object passed
if(typeIs !== 'object' && typeIs !== 'function') {
    throw TypeError();
}

function f() {}; //dummy constructor
f.prototype = p; //prototype
return new f(); //return new constructor
}

var $f0 = {}; 
$f0.x = 1; 
var $g0 = inherit($f0); 
$g0.y = 2;
var $h0 = inherit($g0); 

console.log('x' in $h0); //true
console.log(Object.getOwnPropertyNames($h0.prototype)); //throws error

我遇到的问题是,在我运行inherit函数之后,我无法查找对象的原型属性。

我该如何显示原型对象的属性?

EN

回答 1

Stack Overflow用户

发布于 2012-11-22 03:39:43

您希望使用Object.getOwnPropertyNames(Object.getPrototypeOf($h0))或非标准__proto__ propertyprototype仅是构造函数上的属性的名称。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13500451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档