On the understanding of constructor and prototype

Source: Internet
Author: User

Constructor: The prototype property of each function points to an object that contains a unique non-enumerable property constructor, the value of which is such an object: constructor points to its constructor
Prototype: Each function contains a Prototype attribute, which points to a reference to an object, whereas an instance of each function (class) inherits properties from the object pointed to by the Prototype property. In other words, all objects created by the same function inherit an identical object.

function person (name) {this.name = name;}; Person.prototype.getName = function () {return this.name;};    var p = new Person ("Zhangsan"); Console.log (Person.prototype)

constructor function
function person (name) {
THIS.name = name;
Define the prototype of the person, and the properties in the prototype can be referenced by the custom object
Person.prototype = {
Getname:function () {
return this.name;
//Person.prototype.constructor=person Specifies constructor, the instantiated class points to person, and does not point to Object
var Zhang = new person ("Zhangsan");
Console.log (Person.prototype)

On the understanding of constructor and prototype

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.