Javascript 有没有办法显示所有新对象';谁的财产?
假设我有这个代码:Javascript 有没有办法显示所有新对象';谁的财产?,javascript,object,console.log,Javascript,Object,Console.log,假设我有这个代码: x = "abcde"; y = new String(x); 当Iconsole.log(y)时,它只返回以下内容: 字符串{0:a',1:b',2:c',3:d',4:e',长度:5,[[PrimitiveValue]]:“abcde”} 但我仍然可以说y.charAt(0)
x = "abcde";
y = new String(x);
当Iconsole.log(y)
时,它只返回以下内容:
字符串{0:a',1:b',2:c',3:d',4:e',长度:5,[[PrimitiveValue]]:“abcde”}
但我仍然可以说
y.charAt(0)它将返回“a”
,那么charAt
方法隐藏在哪里?如何在console.log中查看它?y
从String.prototype
中获取charAt
,在许多浏览器中,您可以在控制台中看到它。在控制台中,如果它在那里,您可能可以展开\uuuuu proto\uuuu
以查看字符串.prototype
属性(然后展开它的\uuuuu proto\uuu
以查看对象.prototype
属性)
ES6中的JavaScript中添加了\uuuu proto\uuuu
属性,但它可能不存在于对象上,因为它来自object.prototype
,并且对象可能不受object.prototype
的支持(直接或间接)(见下面的条下)
如果要访问代码中的那些属性(包括在控制台中用于检查对象的代码),可以使用(无论对象是否具有\uuuuu proto\uuuuu
)和。前者为您提供对象的原型,后者为您提供该对象的所有属性名(即使它们不可枚举)
因此,对于所有属性的综合列表:
function getAllProperties(obj) {
var props = [];
while (obj) {
props.push.apply(props, Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
}
return props;
}
现场示例:
var x=“abcde”;
变量y=新字符串(x);
log(getAllProperties(y).join(“,”);
函数getAllProperties(obj){
var-props=[];
while(obj){
props.push.apply(props,Object.getOwnPropertyNames(obj));
obj=Object.getPrototypeOf(obj);
}
返回道具;
}
以下是如何在console中找到它
您没有注意到控制台中的\uuuuuu proto\uuuuuuu
吗?您可以使用for-in循环来命中控制台隐藏的所有继承属性。@saptalconsole.log(y.\uuuu proto\uuuu)代码>返回字符串{length:0,[[PrimitiveValue]]:“”}