F.prototype
On this page
JavaScript F.prototype
JavaScript allows creating objects using a constructor function, like the new F(). (JavaScript允许使用构造函数创建对象,如新的F ()。)
Once F.prototype is an object, the new operator may use it for setting [[Prototype]] for the new object. (一旦F.prototype是一个对象,新运算符就可以使用它来为新对象设置[[Prototype]]。)
Please, take into consideration that here F.prototype means an ordinary property, known as “prototype” on F. It may sound similar to the term “prototype.” Still, here it is intended an ordinary property with this name. (请注意,这里的F.prototype是指普通财产,在F上称为“原型”。它可能听起来类似于“原型”一词。“不过,这里是用这个名字的普通房产。)
For instance:
let animal = {
speaks: true
};
function Dog(name) {
this.name = name;
}
Dog.prototype = animal;
let dog = new Dog("Cute dog"); // dog.__proto__ == animal
console.log(dog.speaks); // true
You can use the F.prototype property only in case the new F is called. It can assign the [[Prototype]] of the new object. Then, no connection between the F.prototype and the new object can be found. We can describe it as a “one-time present.” (只有在调用新F时,才能使用F.prototype属性。它可以分配新对象的[[Prototype]]。然后,在F.prototype和新对象之间找不到连接。我们可以将其描述为“一次性礼物”。)
In case, after creating, F.prototype property changes, (F.prototype = <another object>). Afterward, new objects that the new F has created may have another object, such as [[Prototype]]. But the objects that already exist keep the old one.
Default F.prototype, Constructor Property
Default F.prototype, Constructor Property (默认F.prototype ,构造函数属性)
In JavaScript, each function obtains the “prototype" property. In general, “prototype” is an object with a single property constructor pointing back to the function itself. (在JavaScript中,每个函数都获取“prototype”属性。 通常, “prototype”是一个具有单个属性构造函数指向函数本身的对象。)
Here is an example:
function Dog() {}
/* default prototype
Dog.prototype = { constructor: Dog };
*/
You can check it:
function Dog() {}
// by default:
// Dog.prototype = { constructor: Dog }
console.log(Dog.prototype.constructor == Dog); // true
As a rule, in case of not doing anything, the constructor property is available to all the dogs via [[Prototype]] . (通常,在不做任何事情的情况下,构造函数属性可通过[[Prototype]]供所有狗使用。)
For instance:
function Dog() {}
// by default:
// Dog.prototype = { constructor: Dog }
let dog = new Dog(); // inherits from {constructor: Dog}
console.log(dog.constructor == Dog); // true (from prototype)
It is possible to use the constructor property for creating a new object with the constructor like the existing one. (可以使用构造函数属性使用构造函数创建新对象,就像现有对象一样。)
It is visualized in the following example:
function Dog(name) {
this.name = name;
console.log(name);
}
let dog1 = new Dog("Cute Dog");
let dog2 = new dog.constructor("Little Dog");
A notable thing about “constructor” is that JavaScript gives no guarantees for the right “constructor” value. Of course, it exists in the default “prototype” for the functions. That’s all. Anything happening with it later depends on the developer. (关于“构造函数”的一个值得注意的事情是, JavaScript不保证正确的“构造函数”值。当然,它存在于函数的默认“原型”中。仅此而已。以后发生的任何事情都取决于开发人员。)
For example, in case you decide to replace the default prototype as a whole, there won’t be any “constructor” in it. (例如,如果您决定整体替换默认原型,则其中不会有任何“构造函数”。)
It is demonstrated here:
function Dog() {}
Dog.prototype = {
run: true
};
let dog = new Dog();
console.log(dog.constructor === Dog); // false
Hence, for keeping the right “constructor”, you can use the add or remove properties to the default “prototype” and not write it as a whole. (因此,为了保留正确的“构造函数” ,您可以使用默认“原型”的add或remove属性,而不是将其作为一个整体写入。)
Here is an example:
function Dog() {}
// Not overwrite Dog.prototype totally
// just add to it
Dog.prototype.run = true
// the default Dog.prototype.constructor is preserved
There is an alternative option, as well. It is possible to recreate the constructor property manually, like this:
Dog.prototype = {
jumps: true,
constructor: Dog
};
// now constructor is also correct because we added it
Summary
Summary (概要)
In this chapter, we represented the way to set a [[Prototype]] for the objects made with a constructor function. (在本章中,我们介绍了为使用构造函数创建的对象设置[[Prototype]]的方法。)
Briefly, the F.prototype property is capable of setting [[Prototype]] of new objects when new F() is called. (简而言之,当调用new F ()时, F.prototype属性能够设置新对象的[[Prototype]]。)
The F.prototype value can be an object or null. The special effect is only active for the “prototype” property when it is set on a constructor function and called with new. (F.prototype值可以是对象或null。只有在构造函数上设置并使用new调用“prototype”属性时,特殊效果才会激活。)
As a rule, all the functions have F.prototype = { constructor: F }. Hence it is possible to get the object constructor through accessing its “constructor” property.