Наследование
<html>
<head>
<title>Inheritance demo</title>
</head>
<body>
<script type="text/javascript">
var Class = function(parent,methods){
if (arguments.length == 1) {
methods = parent;
parent = null;
}
var child = function() {
this.initialize.apply(this, arguments);
};
if (typeof(parent) == 'function') {
child.prototype = new parent;
child.superclass = parent;
}
if (typeof(methods) != 'undefined' && methods !== null) {
for (var prop in methods) {
if (!child.prototype[prop] && typeof(methods[prop]) == 'function') {
child.prototype[prop] = methods[prop];
}
}
}
if (!child.prototype.initialize) child.prototype.initialize = function(){};
child.prototype.constructor = child;
return child;
}
var baseClass = Class({
initialize:function(text){
this.text = text;
},
display: function(){
alert(this.text);
}
});
var childClass = Class(baseClass,{
initialize:function(){
this.superclass("base class text");
},
display2: function(){
alert('child class text');
}
});
var obj = childClass();
obj.display();
obj.display2();
</script>
</body>
</html>
Может кто нибудь мне объяснить почему этот код не работает? |
догадываюсь, потому что в нём ошибка? ;)
|
Ага, и ещё она между 1-ой и 58-ой строкой кода.
|
Круто, но мне не такой ответ нужен
Зачем оставлять бессмысленные посты
|
Работает.
|
Цитата:
|
Цитата:
Цитата:
this.superclass.prototype.initialize.call(this, "base class text"); Ну и смена сигнатуры метода в дочернем классе дурной тон. |
Цитата:
|
Цитата:
Цитата:
|
Цитата:
|
Цитата:
|
Цитата:
Цитата:
|
Цитата:
Цитата:
|
| Часовой пояс GMT +3, время: 11:19. |