Javascript field value, private variable, static method declaration

Source: Internet
Author: User
Function newClass (){
This. firstName = "Frank ";
// Declares an instance field. new newClass (). firstName can be accessed only after the new object is created.
// NewClass. firstName cannot be accessed
Var lastName = "Zammetti ";
// Declares a private variable, which can only be accessed within the object;
This. getLastName = function (){
// Declares an instance field, which is an anonymous function with the scope within the current object.
// Compared with the object of new newClass (), this anonymous function is inside it.
Return lastName;
}
This. getId = function (){
Return this. id;
// The value cannot be obtained because the nc to which this points does not have the id attribute.
// Return id cannot be obtained either, because no id variable exists in the anonymous function.
}
}
NewClass. id = 1; // declare a static method of the object; the object cannot be released by the instance;
NewClass. prototype. pid = 9; // declare an attribute on the prototype chain and obtain

Var nc = new newClass (); // return the Instance Object
Alert (nc. firstName); // obtain a field of the Instance Object
Alert (nc. lastName );
Alert (nc. getLastName (); // executes a method of the Instance Object
Alert (nc. id );//
Alert (nc. getId (); // executes a method of the Instance Object and cannot obtain the value of newClass. id.
Alert (nc. pid); // although not declared, this field attribute can be obtained through the prototype chain.
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.