JavaScript static methods and prototyping methods

Source: Internet
Author: User


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>

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.