Показать сообщение отдельно
  #10 (permalink)  
Старый 24.08.2018, 13:01
Аватар для Aetae
Тлен
Отправить личное сообщение для Aetae Посмотреть профиль Найти все сообщения от Aetae
 
Регистрация: 02.01.2010
Сообщений: 6,493

MallSerg, он хочет примерно такую наркоманию:
function CreateTest(name) {
    this.name = name;
    this.roll = () => ["simple", "hard"][Math.round(Math.random())];

    /*
// объявление объекта внутри конструктора работает
    this.renameObj = {
        rename: function() {
            return this.name = `${this.roll()} ${this.name}`;
        }.bind(this)
    };
*/
}

Object.bindedAppend = function(parent, name, child){
    Object.defineProperty(parent.prototype, name, {
        configurable: true,
        enumerable: true,
        get: function(){
            if(this === parent.prototype)
                return child;

            return this[name] = new Proxy(this, {
                get: (p, key) => key in child ? child[key] : this[key],
                set: (p, key, val) => this[key] = val
            })
        }
    });
    return child                    
};


Object.bindedAppend(CreateTest, 'renameObj', {
    rename: function() {
        return this.name = `${this.roll()} ${this.name}`;
    }
});

var test3 = new CreateTest("js test");
test3.renameObj.rename();

alert(test3.name)
__________________
29375, 35
Ответить с цитированием