A concise summary of prototype and constructor in JavaScript

Source: Internet
Author: User
Tags constructor

  has not been able to understand the prototype and constructor properties in JavaScript, read the book today, finally a bit of a figure.

The value of a constructor constructor is a function. In JavaScript, values, arrays, functions, and objects, except null and undefined, have a constructor property, and the value of the constructor property is the constructor of that value, array, function, or object. such as:   code as follows: var a = 12,//number     b = ' str ',//string     C = False,//boolean     d = [1, ' d ' , function () {return 5;}],//array     e = {name: ' E '},//object     F = function () {return ' function ' ; }; Functions   Console.log (' A: ', a.constructor); Number () Console.log (' B: ', b.constructor); String () Console.log (' C: ', c.constructor); Boolean () Console.log (' d: ', d.constructor); Array () Console.log (' E: ', e.constructor); Object () Console.log (' F: ', f.constructor); function ()     The constructors above are JavaScript-built, and we can customize the constructor, such as:     code as follows: function A (name) {    thi S.name = name; }   var a = new A (' a ');   Console.log (A.constructor); A (name)     Call the constructor, you need to use the New keyword, the constructor returns an object, look at the following code to know:   code is as follows: var a = 4; var B = new number (4);   Console.log ('A: ', typeof a); A:number Console.log (' B: ', typeof B); B:object     II, prototype prototype is a property of a function, by default, the value of a function's prototype property is an empty object with the same name as the function. The prototype property of an anonymous function is named object. such as:     code as follows: function fn () {}   Console.log (Fn.prototype); The FN {}     prototype property is primarily used to implement inheritance in JavaScript, such as:   code as follows: function A (name) {    this.name = name; }   A.prototype.show = function () {    console.log (this.name);   function B (name) {    this.name = name;   B.prototype = A.prototype;   var test = new B (' Test ');   Test.show (); Test     Here's a question, the constructor of test is actually a function rather than B function:     code is as follows: Console.log (Test.constructor); A (name)   This is because B.prototype = A.prototype the B.prototype constructor to a, so you need to restore the B.prototype constructor: The   code is as follows: function A ( Name {    this.name = name;}   a.prototype.show = function () {    console.log (this.name);   function B (name) {    this.name = name;}   B.prototype = A.prototype; B.prototype.constructor = B;   var test = new B (' Test ');   Test.show (); Test Console.log (Test.constructor); B (name)     This is because the value of prototype is an object, and its constructor is the value of its constructor property is the function of which it is located, namely:   code as follows: Console.log ( A.prototype.constructor = = A); True  
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.