//方法调用模式
let myObject = {
value: 0,
increment: function (inc) {
this.value += typeof inc === 'number' ? inc : 1;
}
}
myObject.increment(5);
console.log(myObject.value);
function add(a, b) {
return a + b;
}
//函数调用模式
myObject.double = function () {
let that = this;
let helper = function () {
that.value = add(that.value, that.value); //若用this则绑定到全局对象
}
helper();
}
myObject.double();
console.log(myObject.value);
//构造器调用模式
let Quo = function (string) {
this.status = string;
}
Quo.prototype.get_status = function () {
return this.status;
}
let myQuo = new Quo('ok')
console.log(myQuo.get_status());
//apply调用模式
let statusObject = {
status:'ok'
}
let status = Quo.prototype.get_status.apply(statusObject);
console.log(status);
网友评论