1. Create a new object using the Object.create() method and use the existing object to provide the proto of the new object. 2. Provide two parameters, the first is the newly created prototype object, and the second is the object to add properties to the newly created object. Examples//father object let father = { name: 'father', friend: ['abby', 'bob'] } // Generate a new instance object child1 let child1 = Object.create(father) // Change the value type attribute child1.name = 'Modified name' console.log(child1.name) //Modified name // Change the reference type value child1.friend.push('chely') console.log(child1.friend) //[ 'abby', 'bob', 'chely' ] // Generate a new instance object child2 let child2 = Object.create(father) console.log(child2.name) //father console.log(child2.friend) //[ 'abby', 'bob', 'chely' ] Knowledge point expansion: Object.create() creates a method instance const person = { isHuman: false, printIntroduction: function() { console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`); } }; const me = Object.create(person); me.name = 'Matthew'; // "name" is a property set on "me", but not on "person" me.isHuman = true; // inherited properties can be overwritten me.printIntroduction(); // expected output: "My name is Matthew. Am I human? true" Operation Results
This is the end of this article about the detailed usage of Object.create instance in js. For more information about what is the Object.create method in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to locate MySQL slow queries
Table of contents Development Environment Game en...
The basic structure of HTML hypertext documents is...
MySQL password modification example detailed expl...
There are two ways to deploy Angular projects wit...
Table of contents Preface first step: Step 2: Mod...
The party that creates a new connection is equiva...
Preface When the system space usage is too large ...
In the actual project development process, the pag...
Table of contents 1. Structure string 2. Return t...
The MySQL slow query log is very useful for track...
1. View the types of fields in the table describe...
Seurat is a heavyweight R package for single-cell...
This article example shares the specific code of ...
In SQL, GROUP BY is used to group data in the res...
1. What are CSS methodologies? CSS methodologies ...