The differences between the properties and methods defined by the JavaScript constructor class and the prototype prototype

Source: Internet
Author: User

1, the method written in the prototype than write in the constructor consumes less memory, because in the memory of a class prototype only one, the behavior written in the prototype can be shared by all instances, the instance will not be copied in the memory of the instance of a copy
The method that is written in the class, when instantiated, is duplicated in each instance, so the memory consumed is higher
So for no special reason, we generally write attributes into the class, and the behavior is written into the prototype.
2. The properties and methods defined in the constructor are higher than the attributes and methods defined in the prototype, and if the properties and methods of the same name are defined, the constructors will overwrite the
The code is as follows:

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Untitled Document</title></Head><Body><Scripttype= "Text/javascript">//writing the method in the prototype consumes less memory than writing in the constructor, because there is only one prototype in memory, and the behavior that is written in the prototype can be shared by all instances and is not duplicated in the instance's memory .//The method that is written in the class, when instantiated, is duplicated in each instance, so the memory consumed is higher//so for no special reason, we generally write attributes into the class, and the behavior is written into the prototype .functionABC (name) { This. Name=name; //The properties and methods defined in the constructor are higher than the attributes and methods defined in the prototype, and if the properties and methods of the same name are defined, the constructor will overwrite the prototype, so the following X.sya () pops up "My name is Xudeming", if you remove this sentence, The pop-up was "My name is XX";    //the priority of the method is also the same    /*this.say=function () {alert (1); }*/}abc.prototype={name:"xx", say:function() {alert ("My name is"+ This. Name); }}varx=NewABC ("xudeming"); X.say ()</Script></Body></HTML>

Reprinted from: http://www.css119.com/archives/1446

The differences between the properties and methods defined by the JavaScript constructor class and the prototype prototype

Related Article

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.