JS: Как заменить свойство объекта при вызове его метода?

LH
На сайте с 26.09.2013
Offline
89
874

	const Some = {

hello: 'hello',
sayHello: function() { console.log( this.hello ); },

init: function() {
self = this;
document.querySelector( '.some-btn' ).addEventListener( 'click', function() {
self.sayHello();
}, false );
}
}

Some.init();

Хочу заменить свойство объекта. Что-то типа того.

Some.init({

hello: 'hello2'
});
I2
На сайте с 07.03.2015
Offline
38
#1


var Some = function (obj) {
this.checkProp = function() {
if(obj) {

for (var prop in obj) {
if (this.hasOwnProperty(prop)) {
this[prop] = obj[prop];
}
}

}

}
this.hello = 'hello',
this.sayHello = function() { console.log( this.hello ); },

this.init = function() {
self = this;
this.checkProp();
self.sayHello();
}
}
var mySome = new Some({
hello: 'hello2'
})
mySome.init();

Не совсем понятно что вы хотите, но можете попробовать так.

Или так

const Some = {

hello: 'hello',
sayHello: function() {
console.log( this.hello );
},

init: function(setting) {
self = this;
this.hello = setting.hello ? setting.hello : this.hello;
self.sayHello();
}
}

Some.init({
hello: 'hello2'
});
Качественная верстка PSD макетов (/ru/forum/974524)

Авторизуйтесь или зарегистрируйтесь, чтобы оставить комментарий