公司在那里我的工作,现在他们正在写某事像这一切的时候...编写代码的JavaScript
function art() {
};
art.prototype = { sth functions, var's }
或
function art() {
};
art.prototype = (function() {
return{
sth functions, vars
}
})();
这个又是什么目的习惯,这是在改变什么?
公司在那里我的工作,现在他们正在写某事像这一切的时候...编写代码的JavaScript
function art() {
};
art.prototype = { sth functions, var's }
或
function art() {
};
art.prototype = (function() {
return{
sth functions, vars
}
})();
这个又是什么目的习惯,这是在改变什么?
原型是JavaScript的核心功能之一。 Prototype基本上是其他对象继承属性的对象。每个对象都有自己的原型属性,从中继承其成员(属性,方法)。
拿这个经典的多态性例如:
// Car Top Class
var Car = function (brand) {
this.brand = brand;
};
Car.prototype.say = function() {
return 'I am ' + this.brand;
};
// Models inheriting from car
function Mercedes() {
this.fourwheels = '4Matic';
};
Mercedes.prototype = new Car('Mercedes-Benz');
function Audi() {
this.fourwheels = 'Quatro';
};
Audi.prototype = new Car('Audi');
// ---
var mercedes = new Mercedes();
var audi = new Audi();
console.log(`${ mercedes.say() } with ${ mercedes.fourwheels }`);
console.log(`${ audi.say() } with ${ audi.fourwheels }`);
..especially通过分配的Car
到Mercedes
原型,我们正在实现产业新的实例看Mercedes.prototype = new Car('Mercedes-Benz');
(奔驰将收到该车的成员)在这个例子中它对多态性起着关键作用。
很好的解释。也许还包括一个链接到stackoverflow文档? https://stackoverflow.com/documentation/javascript/592/inheritance/723/standard-function-prototype#t=201708110825165874623 – evolutionxbox
@evolutionxbox谢谢。似乎没有新的文档链接是可能的。所以不让我保存它并在这里指出我:https://meta.stackoverflow.com/questions/354217/sunsetting-documentation –
唉。悲伤的时刻。我不知道这个决定。无论如何感谢您的考虑。 – evolutionxbox
你知道原型是什么吗? – evolutionxbox
谷歌搜索原型,解释和教程嘉豪。嗯..他们认为这是一个好主意,将'class'添加到JavaScript。 – 2pha