函数混淆的构造函数

costructor of function confusion

在这种情况下

var A = function(){
    this.d = 123;
}
A.prototype.c = 999;
A.prototype.d = 333;
var B = (new A()).constructor;

console.log(""+B);

console.log(new A().d); // 123
console.log(new B().d); // 123
console.log(new A().c); // 999
console.log(Object.getPrototypeOf(new B()).c); // 999 why?

A 和 B 共享相同的构造函数 但是B不是A,为什么和A有相同的原型?

在这种情况下

var A = function(){
    this.d = 123;
}
A.prototype.c = 999;
A.prototype.d = 333;
var B = A.constructor;

console.log(""+B);

console.log(new A().d); // 123
console.log(new B().d); // undefined
console.log(B.d); // still undefined
console.log(new A().c); // 999
console.log(Object.getPrototypeOf(new B()).c); // undefined

B 是 A 而不是他的实例的构造函数

B是什么?如何在没有 A 实例的情况下访问 A 的构造函数?

当您调用 new A() 时,您创建了一个新的 A 对象,其原型为 A.prototype。当您请求 (new A()).constructor 时,您正在从 A 实例的原型链访问 constructor 属性;这将是 A.prototype.constructor.

A本身就是一个Function对象。也就是说:AFunction的一个实例。当您请求 A.constructor 时,您正在从那个 Function 实例的原型链访问 constructor 属性;这将是 Function.prototype.constructor.

在您的第一种情况下,BA 是对完全相同函数的引用。完全可以预期 new A()new B() 的结果具有相同的属性和相同的原型链。

在您的第二个示例中,BFunction 构造函数——即构造函数的函数。调用 new B() 会创建一个新的 Function 对象。因此,new B() 的结果具有 none 与 A 实例相同的属性。

要区分您可能需要查看什么是 A 和什么是 new A():

c = new A(); // This is an instance of A. The constructor property is A.
console.log(c.constructor) // function() { this.d = 123; }
console.log(new c.constructor()) // Creates another instance of A.
console.log(Object.getPrototypeOf(new c.constructor())) // A {c: 999, d: 333}

var c = A; // This is a function. The constructor property is a base Function.
console.log(c.constructor) // function Function() { [native code] }
console.log(new c.constructor()) // Creates instance of base Function.
console.log(Object.getPrototypeOf(new c.constructor())) // function Empty() {}

如果自定义构造函数 (A) 上没有 new 运算符,您就不会创建 A 的实例。

有关 new 运算符的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

尽管已经回答了,但我没有看到明确说明 B 是 A(哎呀,它迟到的 missing apsillers 提到了它):

var A = function(){};
var B = (new A()).constructor;
console.log(B===A,B===A.prototype.constructor
  ,A.prototype.constructor ===A);//true true true
//Something.prototype.constructor is a circular reference as constructor
//references Something
console.log(A.prototype.constructor.prototype.constructor.prototype
  .constructor.prototype.constructor === A);//true

Constructor自带prototype,设置为constructor function,但可以覆盖(一般在继承的时候做,所以继承后一般会看到修复)

Child.prototype=Parent.prototype;
//Child.prototype.constructor now incorrectly refers to Parent
console.log(Child.prototype.constructor===Parent);//true
//repair constructor
Child.prototype.constructor=Child;

更多关于继承、构造函数和原型的信息here

因为所有对象都有一个原型(除非使用 Object.create(null) 创建)所有对象都有一个构造函数 属性 指向创建它们的函数:

console.log([].constructor===Array);
var arr = new [].constructor(1,2,3);//same as new Array(1,2,3)
console.log(arr);//[1,2,3]
//the following temporarily casts the string to String
console.log("hello".constructor===String);//true
//same with numbers
console.log(1..constructor===Number);//true
console.log(function(){}.constructor === Function);//true