On the _javascript techniques of JavaScript static attributes and prototype attributes

Source: Internet
Author: User

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>

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.