Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.getOwnPropertyNames()、for in、Object.keys() 三者区别 #10

Open
ceerqingting opened this issue Jul 3, 2018 · 0 comments
Open

Comments

@ceerqingting
Copy link
Owner

举个栗子:

  var obj = { a: 1 };
  Object.defineProperty(obj, 'b', { value: 2, enumerable: false });
  Object.prototype.c = 3;
  Object.prototype.d = function() { console.log(1) }
  1. Object.getOwnPropertyNames() : 返回对象自有的所有属性,包括可枚举和不可枚举的
Object.getOwnPropertyNames(obj)
[a, b]
  1. for in : 返回对象自有和继承原型的可枚举属性
for( var i in obj ) {
  console.log(i)
}
a
c
d
  1. Object.keys() :返回对象自有可枚举属性
Object.keys(obj)
[a]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant