JS class method, object method, prototype understanding (RPM)

Source: Internet
Author: User

function people (name)
{
This.name=name;
//Object Methods
This . Introduce=function () {
alert ("My name is" +this.name);
}
}
//class method
people.run=function () {
alert ("I can Run");
}
//Prototyping method
people.prototype.introducechinese=function () {
alert ("My name is" +this.name);
}


//Test

var p1=new people ("windking");

P1. Introduce ();

People.run ();

P1. Introducechinese ();




1, object method Understanding is very simple, mainly if the class generates an instance, then the instance can use the method
2, class method, do not need to generate an instance of the method that you can use
3, the prototype method is mainly used to extend the JS existing system objects, such as array array there is no method, you can add a prototype method, then the created array has the method.



1, the object method includes the method in the constructor function and the method of the constructor prototype;
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 is generally used for object instance sharing, such as Person.prototype.sayname=function () {console.log (this.name);} , adding this method to the prototype will enable sharing. This makes it necessary to allocate the appropriate memory for each instance when it is initialized.

JS class method, object method, prototype understanding (RPM)

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.