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("
");
				console.log("
");
				console.log("
");
				console.log("
");
				(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指向,改变作用域

原文地址:https://www.cnblogs.com/MainActivity/p/8478693.html