[英]Prototype object Javascript
When I make a prototype object, and use it for a new instance I should expect the name of the object is equal to how I called it.当我创建一个原型对象并将其用于新实例时,我应该期望对象的名称与我调用它的方式相同。 My prototype is called AUTO and the new one is called BMW
我的原型叫AUTO,新的叫BMW
var auto = function(a,b,c) {
this.a = a;
this.b = b;
this.c = c;
};
var bmw = new auto("blue", true, 123);
console.log(bmw);
So the outpunt of this is: auto {a: "blue", b: true, c: 123}所以输出是: auto {a: "blue", b: true, c: 123}
But I expected BMW {a: "blue", b: true, c: 123}但我预计宝马 {a: "blue", b: true, c: 123}
However when I trigger this piece of code console.log(bmw.a);但是当我触发这段代码时 console.log(bmw.a); i receive the correct value.
我收到正确的值。
So my question is: Why is BMW = auto {a: "blue", b: true, c: 123} and not tot BMW {a: "blue", b: true, c: 123}所以我的问题是:为什么 BMW = auto {a: "blue", b: true, c: 123} 而不是 tot BMW {a: "blue", b: true, c: 123}
Your BMW is an instance of your prototype auto
.您的 BMW 是您的原型
auto
一个实例。 Its name, BMW
, is just the variable in which this instance is stored.它的名称
BMW
只是存储此实例的变量。 Try this for example:试试这个,例如:
var auto = function(a,b,c) { this.a = a; this.b = b; this.c = c; }; var bmw = new auto("blue", true, 123); document.write('<br />What is BMWs type? '); document.write(typeof bmw); document.write('<br />What is autos type? '); document.write(typeof auto); document.write('Is BMW an instance of auto? '); document.write(bmw instanceof auto ? 'Yes' : 'No');
In short, bmw
is the variable that is holding an instance of your prototype Auto
- it is not a new prototype.简而言之,
bmw
是保存原型Auto
实例的变量 - 它不是新原型。 Auto
is the constructor function - it's type is Function
. Auto
是构造函数 - 它的类型是Function
。 Prototypes (or classes in different languages) are simply general objects that define the structure and methods of a certain object by default.原型(或不同语言中的类)只是一般对象,默认情况下定义某个对象的结构和方法。 For instance, you could make a prototype for 'vehicle', and then derive from those two prototypes for two-weeled vehicles and four-wheeled.
例如,您可以为“车辆”制作一个原型,然后从这两个原型中衍生出两轮车辆和四轮车辆。 Then you could make a new four wheeled vehicle, but this time you are making an actual vehicle, giving it a name, etc...
然后你可以造一辆新的四轮车,但这次你是在造一辆真正的车,给它起个名字,等等……
If you want to make a specific prototype for BMW, you should inherit it using the right methods:如果你想为宝马制作一个特定的原型,你应该使用正确的方法继承它:
var bmw = function(a,b,c) {
// Using `call` will run the constructor of auto, with the context of `this`
auto.call(this, a, b, c);
// Then we can do prototype specific things here
this.brand = 'BMW';
};
var bmw = new bmw("blue", true, 123);
Once you add prototype methods, btw, you will also have to use Object.create(auto.prototype)
to chain it correctly.一旦你添加了原型方法,顺便说一句,你还必须使用
Object.create(auto.prototype)
来正确链接它。
Check out MDN for more information - have a good read there: https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain查看 MDN 以获取更多信息 - 请仔细阅读: https : //developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.