标签:des style io ar color os 使用 sp for
1,单例模式<script>//obj为单例var obj = {myProp: "My Value"};var obj1 = obj;var obj2 = obj;console.log(obj1 == obj2);//trueconsole.log(obj1 === obj2);//true</script>
<script>function Universe(){if(typeof Universe.instance === "object"){return Universe.instance;}this.start_time = 0;this.bang = "Big";Universe.instance = this;}var u1 = new Universe();var u2 = new Universe();console.log(u1 === u2);//true</script>
<script>function Universe(){//缓存私有变量var instance = this;this.start_time = 0;this.bang = "Big";//重写构造函数,返回私有变量Universe = function(){return instance;};}var u1 = new Universe();var u2 = new Universe();console.log(u1 === u2);//true</script>
<script>function Universe(){//缓存私有变量var instance = this;this.start_time = 0;this.bang = "Big";//重写构造函数,返回私有变量Universe = function(){return instance;};}Universe.prototype.nothing = true;var u1 = new Universe();Universe.prototype.everything = true;var u2 = new Universe();console.log(u1.nothing);//trueconsole.log(u2.nothing);//trueconsole.log(u1.everything);//undefinedconsole.log(u2.everything);//undefined</script>
function Universe(){//缓存私有变量var instance ;//重写构造函数,返回私有变量Universe = function Universe(){return instance;};//保留原属性Universe.prototype = this;//创建实例instance = new Universe();//重置构造函数指针instance.constructor = Universe;this.start_time = 0;this.bang = "Big";return instance;}Universe.prototype.nothing = true;var u1 = new Universe();Universe.prototype.everything = true;var u2 = new Universe();console.log(u1.nothing);//trueconsole.log(u2.nothing);//trueconsole.log(u1.everything);//trueconsole.log(u2.everything);//true</script>
<script>function CarMaker(){CarMaker.prototype.drive = function(){return "Vroom, I hava " + this.doors + " doors";};//静态工厂方法CarMaker.factory = function(type){var constr = type, newCar;if(typeof CarMaker[constr] !== "function"){throw{name: "Error",message: constr + " doesn‘t exist";};}//工厂方法if(typeof CarMaker[constr].prototype.drive !== "function"){CarMaker[constr].prototype = new CarMaker();}newCar = new CarMaker[constr]();return newCar;};CarMaker.compact = function(){this.doors = 4;};CarMaker.convertible = function(){this.doors = 2;};CarMaker.SUN = function(){this.doors = 24;};}</script>
<script>var o = new Object(),n = new Object(1),s = Object(‘1‘),b = Object(true);console.log(o.constructor === Object);//trueconsole.log(n.constructor === Number);//trueconsole.log(s.constructor === String);//trueconsole.log(b.constructor === Boolean);//true</script>
<script>var element;while(element = agg.next){//业务处理console.log(element);}</script>
<script>var agg = (function(){var index = 0,data = [1, 2, 3, 4, 5],length = data.length;return {next: function(){var element;if(!this.hasNext()){return null;}element = data[index];index = index + 1;return element;},hasNext: function(){return index < length;},rewind: function(){index = 0;},current: function(){return data[index];}};}());agg.rewind();console.log(agg.current());//1while(agg.hasNext()){console.log(agg.next());// 1 2 3 4 5}</script>
<script>var sale = new Sale(100);sale = sale.decorator("fedtax");//增加联邦税sale = sale.decorator("quebec");//增加省税sale = sale.decorator("money");//以美元形式展示sale.getPrice();//"$112.88"//使用不同的税收策略var sale = new Sale(100);sale = sale.decorator("fedtax");//增加联邦税sale = sale.decorator("cdn");//以cdn形式展示sale.getPrice();//"CDN$ 105.88"</script>
<script>function Sale(price){this.price = price || 100;}Sale.prototype.getPrice = function(){return this.price;};//装饰者对象Sale.decorators = {};//装饰者从父对象获取值,然后再修改该值Sale.decorators.fedtax = {getPrice: function(){var price = this.uber.getPrice();price += price * 0.005;return price;}};Sale.decorators.quebec = {getPrice: function(){var price = this.uber.getPrice();price += price * 0.075;return price;}};Sale.decorators.money = {getPrice: function(){return "$" + this.uber.getPrice().toFixed(2);}};Sale.decorators.cdn = {getPrice: function(){return "CDN$" + this.uber.getPrice().toFixed(2);}};Sale.prototype.decorator = function(decorator){var F = function(){},overrides = this.constructor.decorators[decorator],i, newobj;F.prototype = this;newobj = new F();newobj.uber = F.prototype;for(i in overrides){if(overrides.hasOwnProperty(i)){newobj[i] = overrides[i];}}return newobj;};var sale = new Sale(100);sale = sale.decorator("fedtax");//增加联邦税sale = sale.decorator("quebec");//增加省税sale = sale.decorator("money");//以美元形式展示console.log(sale.getPrice());//"$112.88"//使用不同的税收策略var sale = new Sale(100);sale = sale.decorator("fedtax");//增加联邦税sale = sale.decorator("cdn");//以cdn形式展示console.log(sale.getPrice());//"CDN$ 105.88"</script>
标签:des style io ar color os 使用 sp for
原文地址:http://blog.csdn.net/mergades/article/details/41826831