Skip to content

Instantly share code, notes, and snippets.

@villadora
Created July 13, 2012 02:17
Show Gist options
  • Save villadora/3102299 to your computer and use it in GitHub Desktop.
Save villadora/3102299 to your computer and use it in GitHub Desktop.
propertyIsEnumerable & hasOwnProperty

哪些属性会在for... in ...中遍历, 哪些不会,可以通过propertyIsEnumerable进行测试

var a = ['is enumerable'];  
a.propertyIsEnumerable(0);          // returns true  
// 在javascript中array的也是通过hash map实现只不过index换成了number,所以0在这里也是enumerable的属性就可以理解了
a.propertyIsEnumerable('length');   // returns false  

Math.propertyIsEnumerable('random');   // returns false  
this.propertyIsEnumerable('Math');     // returns false  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment