Prototype inheritance
Всем привет,
Собственно получил такой вопрос (см. ниже), и хотел бы уточнить правильно ли я понял задачу, если нет, то какое решение было-бы правильным. Вопрос: 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'); Благодарю Знатоков |
Что-то я не вижу, где ты
Цитата:
ParentClass.prototype.secondMethod(arg1, arg2); садись, 2 :) Цитата:
|
Я хочу получить помощь в вопросе, ответ на который я мог дать неверно.
Хотя-бы что именно в моем ответе не верно? |
Цитата:
Цитата:
2. Я не вижу в твоем коде Цитата:
|
Часовой пояс GMT +3, время: 07:41. |