The
article gives you an example of the static and prototyping methods of JavaScript, and if you don't know about JavaScript's static and prototyping methods, you can look at it together with a small one. A piece of code that understands static methods and instance methods:
<script>//Object constructor function atest (name) {//private property, only use var className = "Atest" within an object constructor;
Public property that calls THIS.name = name after the object is instantiated;
Object Method This.hello = function () {alert (this.name); Alert (this.msg ());//The method of using the prototype method can be used to use alert (This.sex) within the class;//Properties extended using the prototype method can use alert (atest.age) within the class;//static property call-time format is the [object. static property]}}///class method (actually called directly by static method): The external syntax format of the person class: Class name. Method name = function ([parameters ...])
{statement line;}
Atest.run = function () {alert ("I am a class method Run");
}//prototype method Atest.prototype.msg = function () {alert ("My name is:" +this.name);//If the prototype method is called directly as a static method, THIS.name cannot be invoked }//public static property is outside the class Atest.age = 20;//public static properties cannot use "this. Property", only use the "object. Property" Call//prototype property, as a property within the class using "this. prototype properties", and also
You can use the object as a public static property. Prototype. prototype Properties "Atest.prototype.sex =" male "; Atest.run (); Class methods are also static methods that can be used directly by using the object. static method () Atest.prototype.msg ()//The prototype method is used as a static method object. Prototype. Method () alert (Atest.prototype.sex //The prototype property is used as a static Property object. ProtOtype. Method () "var a = new Atest (" Zhangsan ");//Object methods and prototyping methods need to instantiate an object before you can use A.hello ();/object method must instantiate object A.msg ();//The prototype method must be instantiated
Object Alert (A.age)://error, public static properties can only be invoked using the object. Property call//ps: Try to define the method as a prototype method, avoiding the construction of properties or methods each time the constructor is invoked, saving space and creating objects quickly.
</script>