Home  >  Article  >  Web Front-end  >  Analysis of the basic mechanism of JavaScript prototypal inheritance_javascript skills

Analysis of the basic mechanism of JavaScript prototypal inheritance_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:02:59953browse

The essence of this language feature relies on JavaScript's unique prototype chain pattern.
So strictly speaking, JavaScript is an object-oriented language based on prototypes. That is, every instance object has a prototype. Objects inherit properties and methods from this prototype.

1. Constructor

Using the constructor, you can simply create objects. The this keyword in the constructor points to the instance object itself:

Copy code The code is as follows:

function People(name){
this.name = name;
}

Use the new operator and constructor to create instance objects:
Copy code The code is as follows:

var people = new People('Xiao Ming');
console.log(people.name); //Xiao Ming

But if two instances are created, properties and methods cannot be shared directly between the two instances:
Copy code The code is as follows:

var people1 = new People('Xiao Ming');
var people2 = new People('Xiao Wang');
people1.sex = 'male';
console.log(people2.sex); //undefined

That is to say, once the object is instantiated, its attributes and methods exist independently. Modifications to one attribute will not affect other instances.

2. Prototype

So there is the prototype property, which is automatically created when an instance object is generated. It is an object in itself, with properties and methods that can be shared between instances. The properties and methods of the instance itself are included in the constructor. In other words, the properties and methods inside the constructor become local properties and methods after instantiation, while the properties and methods in the prototype are only references in the instance, so they can be used by multiple instances. shared.

It’s the same constructor just now, now add the prototype attribute to it:
Copy the code The code is as follows:

People.prototype.sex = 'female';
//Or written as People.prototype = {sex: 'female'};
console.log(people1.sex); // male
console.log(people2.sex); //female

The prototype attribute parameter of the People constructor will directly affect the two instances of people1 and people2.

But why does people1.sex output male? This is because in JavaScript, prototype relationships exist in a recursive form. An object's prototype is also an object, and a prototype itself may have a prototype. The highest level of prototype is the global Object object.

This means that once people1.sex is set to male, its corresponding value in the prototype cannot be exposed. If people1.sex itself has no value, it will be read from the prototype property of the constructor, and so on, searching upwards one level at a time until the Object object.

Note: Using "null" to assign a value to an object can destroy the custom object and release memory resources.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn