New in JavaScript using _javascript tips

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.