The difference between JS object method, class method, prototype method, private property, public property, public static property

Source: Internet
Author: User

1 <HTML>2     <Head>3     <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">4         <title>Test</title>5     </Head>6     <Body>7         <Script>8         //Object Constructors9         functionatest (name) {Ten             //private properties, which can only be used inside the object constructor One             varClassName= "atest"; A             //Public property, called after an object instantiation -              This. Name=name; -             //Object Methods the              This. Hello= function(){ - Alert ( This. Name); - Alert ( This. MSG ());//methods that are expanded using the prototype method can be used inside the class - Alert ( This. Sex);//properties that are expanded using the prototype method can be used inside the class + alert (atest.age);//a static property is called when the format is [object. Static property] -             } +         } A         //the location of the class method (which is actually called directly by a static method): The external syntax format for the Person class: Class name. Method name = function ([parameter ...]) {statement line;} at Atest.run= function(){ - Alert ("I am the class method Run"); -         } -   -   -         //Prototyping Methods in 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 called to         } +   -         //public static properties outside of class the Atest.age=  -;//public static properties cannot use the "this. Property" and can only be called by using the object. property. *   $         //The prototype property, which is considered to be a property inside the class, uses the "this. prototype attribute", or it can be used as a public static property "object. prototype. prototype Properties"Panax Notoginseng Atest.prototype.sex= "male"; -   the Atest.run ();//class methods are also static methods that you can use directly with the object. static method () + Atest.prototype.msg ();//The prototype method is used as a static method when using the object. Prototype. Method () " A alert (Atest.prototype.sex);//The prototype property is treated as a static property when you use the object. Prototype. Method () " the         vara= NewAtest ("Zhangsan");//object methods and prototype methods need to instantiate objects before they can be used + A.hello ();//Object methods must instantiate objects - a.msg ();//The prototype method must instantiate the object $ alert (a.age)://error, public static property can only be invoked using the object. Property $   -         //PS: As far as possible the method is defined as a prototype method, the prototype method avoids each call to the constructor of the property or method construction, save space, create objects faster. -     </Script> the     </Body> - </HTML>

Reprinted from: Http://my.oschina.net/tongjh/blog/265087?p={{currentpage-1}}

The difference between JS object method, class method, prototype method, private property, public property, public static property

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.