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指向,改变作用域

posted @ 2018-02-27 14:22  飞尽堂前燕  阅读(114)  评论(0编辑  收藏  举报