Detailed explanation of Object.create instance usage in js

Detailed explanation of Object.create instance usage in js

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

> "My name is Matthew. Am I human? true"

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:
  • A new way to create JavaScript objects: Object.create()
  • Introduction to creating objects using Object.create() in JavaScript
  • Introduction to JavaScript Design Pattern Prototype Pattern (Object.create and prototype)

<<:  How to locate MySQL slow queries

>>:  Detailed explanation of the difference between url ending with / and without / in nginx proxy_pass configuration

Recommend

Windows10 mysql 8.0.12 non-installation version configuration startup method

This article shares the specific steps for config...

Canvas draws scratch card effect

This article shares the specific code for drawing...

How to use regular expression query in MySql

Regular expressions are often used to search and ...

Using Openlayer in Vue to realize loading animation effect

Note: You cannot use scoped animations! ! ! ! via...

Detailed explanation of various HTTP return status codes

When a request is sent to your server to display ...

Tutorial on installing MySQL with Docker and implementing remote connection

Pull the image docker pull mysql View the complet...

WebWorker encapsulates JavaScript sandbox details

Table of contents 1. Scenario 2. Implement IJavaS...

Solution to the problem of mysql master-slave switch canal

After configuring VIP, the error message that app...

How to write a MySQL backup script

Preface: The importance of database backup is sel...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

Solving problems encountered when importing and exporting Mysql

background Since I converted all my tasks to Dock...

Detailed tutorial on building nextcloud private cloud storage network disk

Nextcloud is an open source and free private clou...

How to compile the Linux kernel

1. Download the required kernel version 2. Upload...

Vue uniapp realizes the segmenter effect

This article shares the specific code of vue unia...