JS class method, object method, prototype method

Source: Internet
Author: User

1, the object method: Includes the method in the constructor function and the constructor function prototype above method;
2, class method: In fact, the class here is a function. In JS because the function is also an object, so you can add properties and methods for the function, this method in node with more;
3, the prototype method: commonly used for object instance sharing, in the prototype above to add the method, you can realize the sharing. This makes it necessary to allocate the appropriate memory for each instance when it is initialized.
When a function in JavaScript is a constructor, it is a class that, with the new operator, can return an object.

function people (name) {      this.name=name;      Object method This      . Introduce=function () {          alert ("My name is" +this.name);      }  }  Class method  People.run=function () {      alert ("I can Run");  }  Prototype method  People.prototype.introducechinese=function () {      alert ("My name is" +this.name);  }    Test  var p1=new people ("windking");  P1. Introduce ();        The object method needs to invoke People.run () by instantiating the object  ;          The class method does not need to invoke P1 by instantiating the object  . Introducechinese (); The prototype method also needs to be invoked by instantiating the object    

  

JS class method, object method, prototype method

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.