Answer the question
In order to leave comments, you need to log in
How to deal with the prototype and the constructor?
Please help me understand this code. I think I don't understand something.
The prototype property is an intermediate object. Explain line by line please
function extend(Child, Parent) {
var F = function() { }
F.prototype = Parent.prototype
Child.prototype = new F()
Child.prototype.constructor = Child
Child.superclass = Parent.prototype
}
Answer the question
In order to leave comments, you need to log in
var F = function() { }
F.prototype = Parent.prototype
Child.prototype = new F()
var iLoveToMakeChild1 = Object.create(Child.prototype)
var iLoveToMakeChild2 = new Child()
function Млекопитающее() {
//И вот тут может произойти непонятное без прокладки F
}
Млекопитающее.prototype.commonParams = [
'Вскармливает детей молоком',
'Теплокровное'
];
function Человек(options) {
if (options && typeof options == 'object') {
for (var i in options) {
this[i] = options[i];
}
}
}
function Кошка(options) {
if (options && typeof options == 'object') {
for (var i in options) {
this[i] = options[i];
}
}
}
function extend(Child, Parent) {
var F = function() { };
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype;
}
extend(Человек, Млекопитающее);
Человек.prototype.ownParams = [
'Носит одежду',
'Поёт в караоке'
];
extend(Кошка, Млекопитающее);
Кошка.prototype.ownParams = [
'Забавно урчит',
'Ест мух'
];
var man1 = new Человек({
name: 'Петя',
params: [
'Брюнет',
'Любит Хауса',
'Бросил курить'
]
}),
animal1 = new Кошка({
name: 'Маська',
params: [
'Любит кукурузу',
'Имеет 2 лотка'
]
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question