首页 > 编程语言 > 详细

javascript设计模式1

时间:2014-10-08 23:23:57      阅读:302      评论:0      收藏:0      [点我收藏+]

带原型的Constructor模式

function Car(model, year, miles){

  this.model = model;

  this.year = year;

  this.miles = miles;

}

Car.prototype.toString = function(){

  return this.model + " has done " + this.miles + " miles";

};

var civic = new Car("Honda Civic", 2009, 2000);

alert(civic.toString()); 

 

Module 模式

1. 对象字面量表示法 2. Module模式 3. AMD 模块 4. CommonJS模块 5. ECMAScript Harmony 模块

# 对象字面量表示法

var myModule = {

  myProperty: "someValue",

  myConfig: {

    language: "en"

  },

  myMethod: function(){

    alert(‘xxx‘);

  },

  myMethod2: null

}

# Module 模式

var testModule = (function(){
  var counter = 0;

  return {

    increment: function(){

      return ++counter;

    },

    reset: function(){

      counter = 0;

    }

  };

})();

javascript设计模式1

原文:http://www.cnblogs.com/terabithia/p/4008892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!