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