js call() 笔记

var ctrl = function() {};
ctrl.view = function() {
return {
show: function() {
console.log("view show");
},
hide: function() {
console.log("view hide");
}
}
}();
ctrl.methods = function() {
return {
create: function() {
console.log('methods create')
},
update: function() {
console.log('methods update')
},
download: function() {
console.log('methods download')
},
init: function() {
(function() {
console.log('------------调用View方法 start-----------------');
this.show();
this.hide();
console.log('------------调用View方法 end-----------------');
console.log("\n");
console.log("\n");
console.log("\n");
console.log("\n");
(function() {
console.log('------------调用Methods方法 start-----------------');
this.create();
this.update();
this.download();
console.log('-------------调用Methods方法 end----------------');
}).call(ctrl.methods);//更改this指向Methods
}).call(ctrl.view);//更改this执行View }
}
}();
ctrl.methods.init();

call更改this指向,改变作用域

上一篇:C语言-05-内存分析


下一篇:LeetCode之LRU Cache 最近最少使用算法 缓存设计