So there is no concept of class in JavaScript. It is important to understand that many JavaScript tutorials are designed to make readers understand more easily by applying the concepts in their commonly used class patterns, which in turn makes them ambiguous.
Advanced Segment Code,
Copy Code code as follows:
function Employee () {
This.name= "";
This.dept= "";
}
employee.prototype={
Say: ' Hello '
}
var p = new Employee ();
This code, we will often use, where the new keyword is most likely to cause confusion in one place. Here's new and. NET does a different job.
We can understand the process of creating an instance in JavaScript using the new action, which creates a new object with employee () for the template, which duplicates the member variables in the employee constructor (or, as an argument, the P object as a parameter in the constructor). And the member variables of all this in the function are applied, and the constructor's prototype is inherited.
We use code to simulate the process of new.
Copy Code code as follows:
var p = new Object () is also correct.
var p ={};
Employee.apply (P);
p.__proto__ = Employee.prototype;
__PROTO__ is an internal property of a JavaScript object instance that points to the prototype property of the constructor, employee, and when the object finds a member variable, For example, P.say, first look for its own member properties, if found, the return value, if not found, to call __proto__ to check the prototype chain, the current example, that is, in Employee.prototype find say members.