A piece of code that understands static methods and instance methods:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>test</title>
<body>
<script>
Object Constructors
function Atest (name) {
Private properties that can only be used within an object constructor
var className = "Atest";
Public property that is called after the object is instantiated
THIS.name = name;
Object methods
This.hello = function () {
alert (this.name);
Alert (this.msg ())//The method augmented using the prototype method can be used inside the class
alert (this.sex);//The properties extended using the prototype method can be used inside the class
alert (atest.age);//static property call when the format is [object. Static Properties]
}
}
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 properties are outside the class
Atest.age = 20;//public static properties cannot use "this. Property" and can only be invoked using the object.
A prototype property, which is used as a property within a class to use the this. prototype property, or as a public static property to use the object. Prototype. Prototype Properties "
Atest.prototype.sex = "male";
Atest.run (); Class methods are also static methods that can be used directly with 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 objects before they can be used
A.hello ()//object method must instantiate object
A.msg ();//The prototype method must instantiate the object
Alert (A.age)://error, public static property can only be invoked using the object. Properties
PS: As far as possible, the method is defined as a prototype method, which avoids the construction of properties or methods each time the constructor is invoked, saves space and creates objects quickly.
</script>
</body>