Introduction to Javascript static attributes and prototype attributes _ javascript skills

Source: Internet
Author: User
This article introduces the static and prototype attributes in javascript, and attaches an example analysis. it is very practical. if you need it, you can refer to it. This article will introduce you to the Javascript static method and prototype method. if you do not know about the Javascript static method and prototype method, you can take a look at it with the editor. A piece of code to understand static methods and instance methods:

Script // object constructor function Atest (name) {// private attribute. you can only use var className = "Atest" within the object constructor; // public attribute, call this after the object is instantiated. name = name; // object method this. hello = function () {alert (this. name); alert (this. msg (); // you can use alert (this. sex); // attributes extended using the prototype method can use alert (Atest. age); // The Static attribute format is [object. static attributes] }}// class method (actually called directly by a static method) Location: The external syntax format of the Person class: class name. method name = function ([parameter...]) {statement line;} Atest. run = function () {alert ("I am a class method Run");} // The prototype method Atest. prototype. msg = function () {alert ("My name is:" + this. name); // If the prototype method is directly called as a static method, this. name cannot be called} // public static attributes are stored in the external Atest of the class. age = 20; // public static attributes cannot use [this. attribute. only objects can be used. properties: call // A prototype property, and use this as an internal property of the class. prototype attribute. you can also use the object as a public static attribute. prototype. prototype attributes: Atest. prototype. sex = "male"; Atest. run (); // The Class method is also a static method. you can directly use [object. static method ()] Atest. prototype. msg (); // when the prototype method is used as a static method [object. prototype. method ()] alert (Atest. prototype. sex); // when the prototype property is used as a static property. prototype. method ()] var a = new Atest ("zhangsan"); // object method and prototype method can be used only after the object is instantiated. hello (); // object method must instantiate object. msg (); // The prototype method must instantiate the object alert (. age): // error. only objects can be used for public static attributes. property: call // ps: define the method as a prototype method whenever possible. The prototype method avoids the construction of attributes or methods during each call to the constructor, saving space and creating objects quickly. script

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.