Methods on Object

Object.create () method to create objects

  • Object.create(proto, [propertiesObject])Method creates a new object, using the existing objects to provide the newly created object __proto__.
  • proto The newly created object's prototype object.
  • propertiesObjectOptional. Is to be added to the newly created object may be enumerated attribute (i.e., its own defined properties, rather than the enumerated attribute prototype chain) attribute descriptor object and the corresponding attribute name. These properties correspond to Object.defineProperties()the second parameter is not specifiedundefined
let proto={
  a:0,
  b:1
};
let o=Object.create(proto,{x:{
  enumerable: false,//对象的枚举属性  默认为false
  configurable: false,//该属性是否能删除或者修改  默认为 false。
  writable: false,//该属性是否能被赋值运算符改变  默认为 false。
  value: "static"//该属性的值  默认为 undefined
}});
console.log(o);
3423165-263b7b295bba64f2.png
console.log(o)

If you create a control object. The object is no prototype property

console.log(Object.create(null));
3423165-b08402f4605e9a61.png
console.log(Object.create(null))

Prototype property Object.setPrototypeOf () and Get Object.getPrototypeOf () object

//创建一个原型对象
let proto={
  x:20,
  y:30
};
//在原型对象上再添加一个不可枚举的属性s
Object.defineProperty(proto,"s",{
  value:90,
  enumerable:false
});
//创建对象
let o={x:50};
//设置对象的proto属性
Object.setPrototypeOf(o,proto);
console.log(o);
3423165-fb35f20525f0ebb4.png
console.log(o)

Gets the object of proto property whether or not enumerable can get to

console.log(Object.getPrototypeOf(o))//{x: 20, y: 30, s: 90}
3423165-538c4781e448d750.png
console.log(Object.getPrototypeOf(o))

Here's look at the string and boolean types prototype object

Object.getPrototypeOf('foo') === String.prototype // true
Object.getPrototypeOf(true) === Boolean.prototype // true

Object.defineProperty (obj, prop, descriptor) method defines an object directly on a new attribute, or modify existing properties of an object and return the object.

  • on which the object obj to be defined in the properties.
  • prop name of the property you want to define or modify.
  • descriptor to be defined or modified attribute descriptor.
  • You can not set accessor (get and set) and wriable or value at the same time, otherwise it will report an error
let o={};
Object.defineProperty(o,"a",{
  enumerable: true,//对象的枚举属性  默认为false  for in 的时候能否被循环到
  configurable: true,//该属性是否能删除或者修改  默认为 false。
  //writable: true,//该属性是否能被赋值运算符改变  默认为 false。
  //value: "a",//该属性的值  默认为 undefined
  set(val){
    this.b=`wo shi ${val}`;
  },
  get(){
    return "wo shi a";
  }
});
o.a=1;
console.log(o);
3423165-62c0d7f1d7706a58.png
console.log(o)

This is to add an attribute to the object itself, the following method is to add a plurality of attributes of objects

Object.defineProperties () methods defined directly on the plurality of new properties or modify existing properties of an object and return the object.

let obj = {get foo(){return 17}};
Object.defineProperties(obj, {
  'property1': {
    value: true,
    writable: true
  },
  'property2': {
    value: 'Hello',
    writable: false
  }
});
console.log(obj);
3423165-bb39fc6ab8d46c08.png
console.log(obj)

They have set up their own property, of course, have acquired their own property, but also there are two ways, one is to obtain one's own property, and the other is to obtain all, we look at the first method

Object.getOwnPropertyDescriptor()

  1. Object.getOwnPropertyDescriptor()Returns the specified method of an object corresponding to a property of its own attribute descriptor. (Own property refers directly assigned to the object's properties, do not need to look for properties from the prototype chain)
    we have to get above the objobject's propertiesfoo
let d=Object.getOwnPropertyDescriptor(obj,"foo");
console.log(d);

We can see this property to get a very detailed descriptors, including get, set function is enumerable, if you can modify or delete


3423165-628ce7ea633ba1ab.png
console.log(d)
  1. Here we use the second method Object.getOwnPropertyDescriptors(o)
    Object.getOwnPropertyDescriptors(obj)is used to get all the own attribute of an object descriptor method.
    We still get all the properties above that object obj
let tempObj=Object.getOwnPropertyDescriptors(obj) 
console.log(tempObj);

By following this picture, we can see that all the properties of the object obj descriptor will get to


3423165-ffaf4260b0e33ecf.png
console.log(tempObj)

hasOwnProperty()

hasOwnProperty() Method returns a Boolean value that indicates whether the object has its own attributes specified attribute, that is, is their own property or inherit property on the prototype

let obj={
  name:"andy",
  age:18
}

obj.__proto__.job="hehe";
for(let name in obj){// for in 可以循环出所有的枚举属性
  if(obj.hasOwnProperty(name)){
    console.log("自身属性"+name);//自身属性name 自身属性age
  }else{
    console.log("原型上继承的属性"+name)//原型上继承的属性job
  }
}

If the specified attribute is its own, it will return true, otherwise it will return false, if we cycle a target when the inheritance of property, but we just want to use the properties of the object itself, you can use hasOwnPropertythis method to determine

Object.getOwnPropertyNames(obj)

Object.getOwnPropertyNames(obj)All property returns a name specified by the object's own properties (including non-enumerable properties but does not include Symbol value as the property name) an array

let obj={
  name:"andy",
  age:18
};
obj.__proto__.job="hehe";
Object.defineProperty(obj,"sex",{
  enumerable:false
});
let arr=Object.getOwnPropertyNames(obj);
console.log(Object.getOwnPropertyNames(obj));//["name", "age", "sex"]

Do not print out jobthis property, so this method will only get their property regardless of whether enumerable

propertyIsEnumerable(proto)

propertyIsEnumerable(proto)Determining whether property enumerable, returns a Boolean value
also continue to use the code above then write

arr.forEach(item=>{
   console.log(obj.propertyIsEnumerable(item));//true true false
});

Each object has a propertyIsEnumerable method. This method can determine whether the object properties can be specified for ... in loop to enumerate, with the exception of property inherited through the prototype chain. If the object attribute is not specified, then this method returns false. If the judgment is inherited property, whether it is enumerable returns false.

console.log(obj.propertyIsEnumerable("hehe"));//false

Reproduced in: https: //www.jianshu.com/p/15f0365af2a5

Guess you like

Origin blog.csdn.net/weixin_33881041/article/details/91259232