New in javascript

Source: Internet
Author: User

Javascript is an object-oriented language based on Prototype, which is different from the familiar. NET and Java language based on Class mode ).

Therefore, javascript does not have the concept of classes. It is very important to understand this. Many javascript tutorials apply the concepts in their common class patterns to make it easier for readers to understand. This leads to ambiguity.

Advanced Code section,

function employee(){
this.name="";
this.dept="";
}
employee.prototype={
say:'hello'
}
var p = new employee();

This code is often used. The new keyword is the most confusing one. The new and. NET jobs here are different.

In this way, we can understand the process of creating an instance using the new operation in javascript. The new Keyword creates a new object using the employee () template, it copies the member variables in the employee Constructor (it can also be understood that the p object is passed into the constructor as a parameter and all the member variables of this in the application function ), it also inherits the prototype of the constructor.

We use code to simulate the new process.

//var p = new Object() is also correct.
var p ={};
employee.apply(p);
p.__proto__ = employee.prototype;

_ Proto _ is an internal attribute of the javascript object instance. It points to the prototype attribute of the constructor, namely, the employee. When the object looks for a member variable, such as p. say: first, find your member attributes. If yes, the returned value is returned. If no, call _ proto _ to check the prototype chain. The current example is in the employee. the say member is found in prototype.

 

 

Address: http://www.cnblogs.com/leo-penguin/archive/2010/03/20/1689048.html

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.