Вход

Просмотр полной версии : Prototype inheritance


Ervin
31.01.2014, 01:15
Всем привет,

Собственно получил такой вопрос (см. ниже), и хотел бы уточнить правильно ли я понял задачу, если нет, то какое решение было-бы правильным.

Вопрос:

Explain how javascript prototypical inheritance differs from classical inheritance. Implement a subclass using javascript using the following API and test script


ParentClass::firstMethod(args...)
ParentClass::secondMethod(args...)
ChildClass::secondMethod(args...) // calls ParentClass::secondMethod(args...)

parentClass = new ParentClass();
childClass = new ChildClass();
parentClass.firstMethod(1,2); // expect console.log('firstMethod 1');
parentClass.secondMethod(1,2); // expect console.log('secondMethod 2');
childClass.secondMethod(3,4); // expect console.log('secondMethod 4'); console.log('thirdMethod 3');




Мой ответ:


var ParentClass = function(args) {

};

ParentClass.prototype = {
constructor: ParentClass,
firstMethod: function(arg1, arg2) {
console.log("firstMethod " + arg1);
},
secondMethod: function(arg1, arg2) {
console.log("secondMethod " + arg2);
}
};

var ChildClass = function(args) {

};

ChildClass.prototype = {
constructor: ChildClass,
secondMethod: function(arg1, arg2) {
ParentClass.prototype.secondMethod(arg1, arg2);

console.log("thirdMethod " + arg1);
}
};

parentClass = new ParentClass();
childClass = new ChildClass();
parentClass.firstMethod(1,2); // expect console.log('firstMethod 1');
parentClass.secondMethod(1,2); // expect console.log('secondMethod 2');
childClass.secondMethod(3,4); // expect console.log('secondMethod 4'); console.log('thirdMethod 3');


Благодарю Знатоков

nerv_
31.01.2014, 01:42
Что-то я не вижу, где ты Explain


ParentClass.prototype.secondMethod(arg1, arg2);
садись, 2 :)

то какое решение было-бы правильным.
Т.е. ты хочешь, чтобы мы прошли собеседование за тебя?

Ervin
31.01.2014, 01:45
Я хочу получить помощь в вопросе, ответ на который я мог дать неверно.

Хотя-бы что именно в моем ответе не верно?

nerv_
01.02.2014, 00:22
Хотя-бы что именно в моем ответе не верно?
ParentClass.prototype.secondMethod(arg1, arg2);
1. На что будет указывать this при таком вызове и на что оно должно указывать?
2. Я не вижу в твоем коде
Prototype inheritance