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.