首页 > 技术文章 > design.js

lanse-yan 2014-09-17 17:43 原文

//模块式开发
var myNamespace = (function () {

var myPrivateVar = 0;

var myPrivateMethod = function  (foo) {
	console.log(foo);
};

return {
	myPublicVar : "foo",
	myPublicFunction : function  (bar) {
		
		myPrivateVar++;
		myPrivateMethod(bar);
	}
};

})();

//原型模式
var myCar = {
name: "Ford Escort",
drive: function () {
console.log("Weeeee, i'm driving...");
},
panic: function () {
console.log("Wait. How do you stop this thing");
}
};

var yourCar = Object.create(myCar);
console.log(yourCar.name);

//命令模式
var CarManager = {
requestInfo: function (model, id) {
return "The information for " + model + "with ID" + id + "is foobar";
},

buyVehicle: function  (model, id) {
	return "You have successfully purchasedItem" + id + ",a " + model;
},

arrangeViewing: function  (model, id) {
	return "You have successfully booked a viewing of" + model + " " + id;
},

execute:function  (name) {
	return CarManager[name] && CarManager[name].apply(CarManager, [].slice.call(arguments, 1));
}

}
CarManager.execute("requestInfo", "Ferrari","12350");

推荐阅读