Цитата:
Цитата:
Цитата:
Цитата:
|
Maxmaxmaximus11
var A = Class.extend(function () { this.method = function () { } }); var B = A.extend(function () { this.method = function () { this.$method == B.prototype.method; // -> fail! } }); var C = B.extend(function () { this.method = function () { this.$method == B.prototype.method; } }); |
![]() спасибо, баг нашел, короч влом фиксить, все ровно это ни кто юзать не будет. но мой способ поддерживает множественное наследование) function Class(){ } Class.extend = function (Prototype) { Prototype.prototype = this.prototype; constructor.prototype = new Prototype(); constructor.extend = this.extend; function constructor() { if (this.constructor) { this.constructor.apply(this, arguments); } for (var key in this) if (this.hasOwnProperty(key) && /(^_)|(_$)/.test(key)) { Object.defineProperty(this, key, { value : this[key], writable : true, configurable: true, enumerable : false }); } } for (var key in constructor.prototype) if (constructor.prototype.hasOwnProperty(key)) { if (/(^_)|(_$)|(constructor)/.test(key)) { Object.defineProperty(constructor.prototype, key, { value : constructor.prototype[key], writable : true, configurable: true, enumerable : false }); } if (key in this.prototype && typeof this.prototype[key] === 'function') { Object.defineProperty(constructor.prototype, '$' + key, { value : this.prototype[key], writable : false, configurable: false, enumerable : false }); } } return constructor; }; //################################# var Animal = Class.extend(function () { this.say = function () { alert('Animal') } }); var Cat = Animal.extend(function () { this.say = function () { this.$say(); alert('Cat') } }); var Ashot = Cat.extend(function () { this.say = function () { this.$say(); alert('Ashot') } }); new Ashot().say(); |
Цитата:
var obj = { method: function() { // тут этот объект вызывает свой супер метод } }; С твоим подходом проблемы начнутся, если вызвать метод в контексте другого объекта. Т.е. "цепочка" супер методов в лучшем случае не будет вызвана, в худшем нарушена. <script src="http://nervgh.github.io/js/yum/yum.js"></script> <script> /********************** * CLASSES * *********************/ /** * Class Animal * @param {Number} legs * @constructor */ function Animal(legs) {} Animal.prototype.destroy = function() { console.log('Animal.destroy()'); }; // ------------------------ // Inheritance Object.inherit(Mammal, Animal); /** * Class Mammal * @constructor */ function Mammal(legs, teeth) { // call the Animal constructor Mammal.super_.apply(this, arguments); } Mammal.prototype.destroy = function() { console.log('Mammal.destroy()'); Mammal.super_.prototype.destroy.call(this); }; // ------------------------ // Inheritance Object.inherit(Cat, Mammal); /** * Class Cat * @constructor */ function Cat(legs, teeth, tails) { // call the Mammal constructor Cat.super_.apply(this, arguments); } Cat.prototype.destroy = function() { console.log('Cat.destroy()'); Cat.super_.prototype.destroy.call(this); }; /******************** * USAGE / INSTANCE ********************/ var cat = new Cat(); cat.destroy(); // Cat -> Mammal -> Animal; console.log('--------------------'); cat.destroy.call(this); // Cat -> Mammal -> Animal; </script> Цитата:
|
ладно ладно) убедили, я просто щас другим занят)) наварганил прямо тут в текстэриа)
|
Цитата:
var A = Class.extend(function () { this.method = function () { } }); var B = A.extend(function () { this.method = function () { return this.$method === A.prototype.method; } }); var C = B.extend(function () { this.method = function () { return this.$method === B.prototype.method; } }); var b = new B(); var c = new C(); console.log(b.method()); console.log(c.method()); |
nerv_, ты написал как должно быть, а я - как происходит при использовании реализации Class.extend Maxmaxmaximus11.
|
Sweet, я говорю, что "fail" происходит правильно в твоем примере.
Или я опять тебя не понял :) |
nerv_, :) Все всё поняли.
|
Часовой пояс GMT +3, время: 12:05. |