grammar

Object.create(proto, [propertiesObject])

Parameter is introduced

  • proto
    • The prototype object for the newly created object
  • propertiesObject
    • This parameter is optional. This parameter represents an enumerable property to be added to the newly created object (a property defined by itself, not an enumerable property on its prototype chain)
    • Second argument corresponding to ** object.defineProperties ()**
  • The return value
    • A new object with the specified stereotype object and properties
  • abnormal
    • If the propertiesObject is specified as a non-object type, TypeError is raised

The return value

  • The return value is an Object
  • The return prototype, Object.__proto__, is specified as proto

Code sample

let proto = {};
let createdObject = Object.create(proto);
console.log(createdObject.__proto__ === proto);
Copy the code
  • The output is true

The second parameter: propertiesObject

Object.defineProperties

grammar

Object.defineProperties(obj, props)

parameter
  • obj
    • Define or modify a property object on obj
  • Props: an object made up of the following keys
    • A different: indicates whether the property can be removed from an object
    • Enumerable: Indicates whether the property can be enumerated
    • Value: indicates the value of the attribute
    • Writable: Whether the value of this property can be changed
    • Get: Acts as the getter function for this property
    • Set: functions as setters for properties
Code sample
let obj = {};
Object.defineProperties(obj, {
    "name": {
        value: "Cat",
        enumerable: true}}); console.log(obj);Copy the code
  • {name: ‘Cat’}

Object.create takes the propertiesObject parameter

let createdObject2 = Object.create({}, {
    "name": {
        value: "Cat",
        enumerable: true}}); console.log(createdObject2.hasOwnProperty("name"));
Copy the code
  • Print the result: true
  • You can see that the parameters defined are properties of the new object itself