- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能告诉我,两者有什么区别hasOwnProperty
和 propertyIsEnumerable
:
function f(){
this.a = 1;
this.b = 2;
this.c = function(){}
}
f.prototype = {
d : 3,
e : 4,
g : function(){}
}
//creating the instance of an object:
var o = new f();
//And here I can't see difference.
//In my opinion they are doing the same thing
console.log("o.hasOwnProperty('a'):", o.hasOwnProperty('a')); //true
console.log("o.hasOwnProperty('b'):", o.hasOwnProperty('b')); //true
console.log("o.hasOwnProperty('c'):", o.hasOwnProperty('c')); //true
console.log("o.hasOwnProperty('d'):", o.hasOwnProperty('d')); //false
console.log("o.hasOwnProperty('e'):", o.hasOwnProperty('e')); //false
console.log("o.hasOwnProperty('g'):", o.hasOwnProperty('g')); //false
console.log("o.propertyIsEnumerable('a')", o.propertyIsEnumerable('a')); //true
console.log("o.propertyIsEnumerable('b')", o.propertyIsEnumerable('b')); //true
console.log("o.propertyIsEnumerable('c')", o.propertyIsEnumerable('c')); //true
console.log("o.propertyIsEnumerable('d')", o.propertyIsEnumerable('d')); //false
console.log("o.propertyIsEnumerable('e')", o.propertyIsEnumerable('e')); //false
console.log("o.propertyIsEnumerable('g')", o.propertyIsEnumerable('g')); //false
如果我错了请纠正
最佳答案
“propertyIsEnumerable”函数始终排除不会为“hasOwnProperty”返回 true
的属性。您没有采取任何措施使任何属性不可枚举,因此在您的测试中结果是相同的。
您可以使用“defineProperty”来定义不可可枚举的属性; see this reference在MDN。
Object.defineProperty(obj, "hideMe", { value: null, enumerable: false });
就像:
obj.hideMe = null;
除非该属性不会显示在 for ... in
循环中,并且使用 propertyIsEnumerable
进行测试将返回 false
。
整个主题是关于旧浏览器中不可用的功能(如果这不是很明显的话)。
关于javascript - hasOwnProperty 与 propertyIsEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10968962/
谁能告诉我,两者有什么区别hasOwnProperty 和 propertyIsEnumerable: function f(){ this.a = 1; this.b = 2; this
下面的正确结果是什么?是否有任何 ECMA 标准规定了这一点?我当前的 Chrome 14.0.835.186m 认为 false 而 Firefox 3.6.22 认为 true。 (new Err
我在 Javascript 代码中遇到了 o.propertyIsEnumerable(x) 方法。我将其理解为 x in o 构造的同义词。有区别吗?如果是这样,您能否在一些实际示例中说明何时使用第
根据 MDN : The propertyIsEnumerable() method returns a Boolean indicating whether the specified proper
我正在努力理解 Enumerability and ownership of properties .它说: Enumerable properties show up in for...in loo
我是一名优秀的程序员,十分优秀!