Javascript里的设计模式

不管你信不信反正js很高深,虽然平时都是getElementById,现在用了jQuery,这东西也很少用了,也就for,if或者字符串操作一下。

本文本着实用的原则,列举了几个常用的js代码写法,让自己的js看起来更优美。

更多详细:http://addyosmani.com/resources/essentialjsdesignpatterns/book/

  1. Constructor Pattern

    function Car( model, year, miles ) {
    
      this.model = model;
      this.year = year;
      this.miles = miles;
    
    }
    
    
    // Note here that we are using Object.prototype.newMethod rather than 
    // Object.prototype so as to avoid redefining the prototype object
    Car.prototype.toString = function () {
      return this.model + " has done " + this.miles + " miles";
    };
    
    // Usage:
    
    var civic = new Car( "Honda Civic", 2009, 20000 );
    var mondeo = new Car( "Ford Mondeo", 2010, 5000 );
    
    console.log( civic.toString() );
    console.log( mondeo.toString() );
    

    
    
    用this声明的就相当于public的
  2. Module Pattern

    var myModule = {
    
      myProperty: "someValue",
    
      // object literals can contain properties and methods.
      // e.g we can define a further object for module configuration:
      myConfig: {
        useCaching: true,
        language: "en"
      },
    
      // a very basic method
      myMethod: function () {
        console.log( "Where in the world is Paul Irish today?" );
      },
    
      // output a value based on the current configuration
      myMethod2: function () {
        console.log( "Caching is:" + ( this.myConfig.useCaching ) ? "enabled" : "disabled" );
      },
    
      // override the current configuration
      myMethod3: function( newConfig ) {
    
        if ( typeof newConfig === "object" ) {
          this.myConfig = newConfig;
          console.log( this.myConfig.language );
        }
      }
    };
    
    // Outputs: Where in the world is Paul Irish today?
    myModule.myMethod();
    
    // Outputs: enabled
    myModule.myMethod2();
    
    // Outputs: fr
    myModule.myMethod3({
      language: "fr",
      useCaching: false
    });
    
    
    var myNamespace = (function () {
    
      var myPrivateVar, myPrivateMethod;
    
      // A private counter variable
      myPrivateVar = 0;
    
      // A private function which logs any arguments
      myPrivateMethod = function( foo ) {
          console.log( foo );
      };
    
      return {
    
        // A public variable
        myPublicVar: "foo",
    
        // A public function utilizing privates
        myPublicFunction: function( bar ) {
    
          // Increment our private counter
          myPrivateVar++;
    
          // Call our private method using bar
          myPrivateMethod( bar );
    
        }
      };
    
    })();
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值