Показать сообщение отдельно
  #26 (permalink)  
Старый 03.11.2010, 02:36
Аватар для x-yuri
Отправить личное сообщение для x-yuri Посмотреть профиль Найти все сообщения от x-yuri
 
Регистрация: 27.12.2008
Сообщений: 4,201

Сообщение от eai
Не работает. Вернее работает, но свойство а ведет себя как статическое свойство. Иными словами оно общее для всех экземпляров "класса" Point.
все, что попадает в прототип - общее для всех экземпляров... до поры до времени
function apply(dst) {
    for (var i = 1; i < arguments.length; i++) {
        for (var prop in arguments[i]) {
            if (arguments[i].hasOwnProperty(prop)) {
                dst[prop] = arguments[i][prop];
            }
        }
    }
    return dst;
}
 
Function.prototype.inherit = function(proto) {
    var that = this;
    proto = proto || {};
    var constructor = proto.hasOwnProperty('constructor') ? proto.constructor : function() { that.apply(this, arguments); };
    var F = function() {};
    F.prototype = this.prototype;
    constructor.prototype = apply(new F(), proto);
    constructor.superclass = this.prototype;
    constructor.prototype.constructor = constructor;
    return constructor;
};

var Point = Object.inherit({
        a: new Array(2),
    constructor: function(ax,ay) {
        this.a[0] = ax;
        this.a[1] = ay;
    }
});

// ---

var a = new Point(1, 2);
var b = new Point(3, 4);
alert(a.a.concat(b.a));
a.a = [5, 6];
alert(a.a.concat(b.a));
Ответить с цитированием