继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Ways to iterate through objects

www说
关注TA
已关注
手记 435
粉丝 83
获赞 493

Relevant basic concepts

Property accessors

let obj = {  a: 'b'};// dot notationconsole.log(obj.a); // 'b'// bracket notationconsole.log(obj['a']); // 'b'// through prototype chainconsole.log(obj.toString, obj.toString === Object.prototype.toString); // ƒ toString() { [native code] } true

Check whether an property exists in object

  • in operator: returns true if the specified property is in the specified object or its prototype chain.

  • hasOwnProperty: returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

let obj = {  a: 'b'};console.log('a' in obj); //trueconsole.log('toString' in obj); //trueconsole.log(obj.hasOwnProperty('a')); //trueconsole.log(obj.hasOwnProperty('toString')); //false

Object.defineProperty && Object.defineProperties - defines new or modifies existing properties directly on an object, returning the object.

let obj = {  a: "b"};Object.defineProperties(obj, {  c: {    value: 'd'
  },  e: {    value: 'f'
  }
});console.log(obj); // {a: "b", c: "d", e: "f"}

getter and setter

We can use getters and setters to generate computed property. E.g.

let people = {  firstName: 'michael',  lastName: 'zheng',
  get fullName() {    return `${this.firstName} ${this.lastName}`
  },
  set fullName(val) {
    [this.firstName, this.lastName] = val.split(' ');
  }
}console.log(people.firstName, people.lastName, people.fullName);//"michael", "zheng", "michael zheng"people.fullName = 'hello world';console.log(people.firstName, people.lastName, people.fullName);//"hello", "world", "hello world"

There are three ways to iterate through objects

let student = {  name: "michael"};Object.defineProperties(student, {  age: {    enumerable: false,    value: 18
  },  grade: {    value: 6,    enumerable: true
  },  sex: {    value: "male",    enumerable: false
  }
});Object.prototype.x = "inherited";

In the sample above, we create an object student. student has following properties:

  • name: self enumerable property

  • age: self non-enumerable property

  • grade: self enumerable property

  • sex: self non-enumerable property

as well as a custom property from prototype chain:

  • x: enumerable

for...in iterates over enumerable properties of an object, including prototype chain.

for (let prop in student) {  console.log(prop); //'name', 'grade', 'x'}for (let prop in student) { // self properties only
  if (student.hasOwnProperty(prop)) {    console.log(prop); // 'name', 'grade'
  }
}

Object.keys returns an array of a given object's property names, only iterate through self enumerable properties.(i.e. not including prototype chain)

console.log(Object.keys(student)); // [‘name’, 'grade']//check whether is plain object:Object.keys(student).length === 0; //falseObject.keys({}).length === 0; //true

Object.getOwnPropertyNames returns an array of all self properties (including non-enumerable properties) found directly upon a given object.

// will not iterate through prototype chainconsole.log(Object.getOwnPropertyNames(student)); // [‘name’, ‘age’, 'grade', 'sex']

Summarize

methodsthrough prototype chainenumerable only
for...inYY
Object.keysNY
Object.getOwnPropertyNamesNN



作者:不吃猫的鱼_zjh
链接:https://www.jianshu.com/p/cc716f9797fa

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP