22

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

如我错了请纠正我

4

4 回答 4

32

“propertyIsEnumerable”函数始终排除不会true为“hasOwnProperty”返回的属性。您没有做任何事情来使任何属性不可枚举,因此在您的测试中结果是相同的。

您可以使用“defineProperty”来定义不可枚举的属性;请参阅 MDN 上的此参考资料

Object.defineProperty(obj, "hideMe", { value: null, enumerable: false });

这就像:

obj.hideMe = null;

除了该属性不会出现在for ... in循环中,并且带有的测试propertyIsEnumerable将返回false

整个主题都是关于旧浏览器中不可用的功能,如果这不明显的话。

于 2012-06-10T13:10:16.827 回答
30

hasOwnProperty甚至会返回true不可枚举的“自己的”属性(如length在 an 中Array)。propertyIsEnumerabletrue仅返回可枚举的“自己的”属性。(“可枚举”属性是显示在for..in循环等中的属性。)

例子:

var a = [];
console.log(a.hasOwnProperty('length'));       // "true"
console.log(a.propertyIsEnumerable('length')); // "false"

或者使用非数组对象:

var o = {};
Object.defineProperty(o, "foo", { enumerable: false });
console.log(o.hasOwnProperty('foo'));       // "true"
console.log(o.propertyIsEnumerable('foo')); // "false"

(当您使用 时Object.definePropertyenumerable默认为false,但为了清楚起见,我在上面已经明确了。)

于 2012-06-10T13:22:28.893 回答
7

简单地说:

hasOwnProperty当且仅当属性是对象的属性并且未被继承时才会返回 true。这个很简单。

propertyIsEnumerable当且仅当hasOwnProperty返回 true 并且该属性是可枚举的时才会返回 true。propertyIsEnumerable测试之上的一项“附加要求”也是如此,如果是hasOwnProperty,名称propertyIsEnumerable会更准确hasOwnPropertyAndIsEnumerable

演示:http: //jsfiddle.net/aby3k/

于 2013-07-06T01:26:11.493 回答
4

不同之处在于 propertyIsEnumerable 仅在属性存在并且可以对属性执行 ForIn 时才返回 true,如果属性存在,hasOwnProperty 将返回 true,而不管 ForIn 是否支持

来自 MSDN:

如果 proName 存在于对象中并且可以使用 ForIn 循环枚举,则 propertyIsEnumerable 方法返回 true。如果对象没有指定名称的属性或指定的属性不可枚举,则 propertyIsEnumerable 方法返回 false。通常,预定义的属性是不可枚举的,而用户定义的属性始终是可枚举的。

如果对象具有指定名称的属性,则 hasOwnProperty 方法返回 true,否则返回 false。该方法不检查该属性是否存在于对象的原型链中;该属性必须是对象本身的成员。

于 2012-06-10T13:10:43.963 回答