The Code is as follows:
<Script language = "javascript" type = "text/javascript">
// (1) convert a method into an object and create methods and attributes for the object
Var Name = function (){
// Create an object using prototype
Name. prototype. Add = function (num, title ){
}
// You can also use this to add the method named object creation method, which is equivalent to the preceding method.
This. Way = function (str ){
}
// Add attribute values for the object
Name. prototype. xing = "123 ";
// Define static attributes and Methods
Name. shi = "static ";
Name. Addd = function (num, title ){
}
// Locally defined static attributes and methods can only be used in static types
Alert (Name. shi );
Name. Addd (1, 2 );
}
// The method can also be declared as follows:
Function Name1 (){
Name1.prototype. add = function (){}
This. way = function (){}
Name1.prototype. shu = "other ";
}
// General global attributes and methods of static definitions
Name. sha = "static ";
Name. Addd2 = function (){
}
Alert (Name. sha); // call static attributes
Name. Addd2 (); // call the static method
Var name = new Name ();
Name. Add (); // object call Method
Name. Way ();
Alert (name. xing); // object call attributes
/* Static global variables can be called outside the method. Static local variables and methods are only used within the method. instance objects cannot call static methods */
/* Prototype cannot be used for instance objects ;*/
// (2) Javascript Object-oriented Inheritance
// Parent class
Function Class (){
This. name = "name ";
This. method = function (){
Alert ("method ");
}
}
// Subclass
Function Class1 (){
This. name1 = "name1 ";
This. method1 = function (){
Alert ("method1 ");
}
}
// Subclass inherits the parent class
Class1.prototype = new Class ();
Var obj = new Class1 ();
Alert (obj. name );
Alert (obj. name1 );
Obj. method ();
Obj. method1 ();
/****** Subclass inherits the syntax of the parent class, subclass. prototype = new parent class ();*****/
// (3) subclass override parent class
// Subclass
Function Class2 (){
This. name2 = "name2 ";
This. method2 = function (){
Alert ("method2 ");
}
}
Class2.prototype = new Class (); // inherit
Class2.prototype. name = "updateName"; // rewrite attributes of the parent class
Class2.prototype. method = function () {// override the method of the parent class
Alert ("UpdateMethod ");
}
Var obj2 = new Class2 ();
Alert (obj2.name); // display updateName
Obj2.method (); // display UpdateMethod
Alert (obj2.name2 );
Obj2.method2 ();
// (4) {} contains objects
Var arr = new Array ();
Arr. push ({"name": "1", "age": 12, funA: function (){}});
Arr. push ({"name": "2", "age": 13 });
Arr. push ({"name": "3", "age": 14 });
For (var I = 0; I <arr. length; I ++ ){
Alert (arr [I]. name );
Alert (arr [I]. age );
Alert (arr [I]. funA ());
}
/*** An object can also be defined as this ***/
Var newObject = {
"Name": "Jim ",
"Sex": "Man ",
Way: function (){}
};
</Script>