设计模式知识连载(3)---封装_2:闭包

来源:互联网 发布:淘宝实物有色差 编辑:程序博客网 时间:2024/06/05 11:51
<body><h3>设计模式知识连载(3)---封装_2:闭包</h3><p>通常将类的静态变量通过闭包来实现</p><script type="text/javascript">    /**    *   利用闭包实现---方式一    */    // var Book = (function () {    //  // 静态私有变量    //  var bookNum = 0 ;    //  // 静态私有方法    //  function checkBook(name) {    //      console.log('执行了静态私有方法checkBook,name:', name) ;    //  } ;    //  // 返回构造函数    //  return function(newId, newName, newPrice) {    //      // 私有变量    //      var num;    //      // 私有方法    //      function checkId(id) {    //          console.log('执行了私有方法checkId,id:', id) ;    //      } ;    //      // 特权方法    //      this.getName = function() {    //          console.log('执行了特权方法:getName') ;    //      } ;    //      this.getPrice = function() {    //          console.log('执行了特权方法:getPrice') ;    //      } ;    //      this.setName = function(name) {    //          this.newName = name ;    //          console.log('执行了特权方法:setName') ;    //      } ;    //      this.setPrice = function(price) {    //          this.newPrice = price ;    //          console.log('执行了特权方法:setPrice') ;    //      }    //      // 公有属性    //      this.id = newId ;    //      this.name = newName ;    //      this.price = newPrice ;    //      // 公有方法    //      this.copy = function() {    //          console.log('执行了公有方法:copy') ;    //      } ;    //      bookNum ++ ;    //      if(100 < bookNum){    //          throw new Erroe('我们仅出版100本书。') ;    //      }    //      // 构造器    //      // this.setName(name) ;    //      // this.setPrice(price) ;    //  } ;    // })() ;    // Book.prototype = {    //  // 静态公有属性    //  isJSBook : false ,    //  // 静态公有方法    //  display : function() {    //      console.log('执行了静态公有方法:display') ;    //  }    // }    // var book = new Book(03, 'javascript设计模式', 399) ;    // console.log('book:', book) ;    // console.log('book.getName:', book.getName) ;    // console.log('book.getPrice:', book.getPrice) ;    // console.log('book.isJSBook:', book.isJSBook) ;    // console.log('book.display', book.display) ;    /**    *   利用闭包实现---方式二    *       *   这种方式看起来更像一个整体    */    var Book = (function(){        // 静态私有变量        var bookNum = 0 ;        // 静态私有方法         function checkBook(name) {            console.log('执行了静态私有方法checkBook', name) ;        } ;        // 创建类        function _book(newId, newName, newPrice) {            // 私有变量            var num ;            // 私有方法            function checkId(id) {                console.log('执行了私有方法checkId', id) ;            } ;            // 特权方法            this.getName = function() {                console.log('执行了特权方法:getName') ;            } ;            this.getPrice = function() {                console.log('执行了特权方法:getPrice') ;            } ;            this.setName = function(name) {                this.newName = name ;                console.log('执行了特权方法:setName') ;            } ;            this.setPrice = function(price) {                this.newPrice = price ;                console.log('执行了特权方法:setPrice') ;            } ;            // 公有属性            this.newId = newId ;            this.newName = newName ;            this.newPrice = newPrice ;            // 公有方法            this.copy = function() {                console.log('执行了公有方法:copy') ;            } ;            bookNum++ ;            if(100 < bookNum) {                throw new Erroe('我们仅出版100本书。') ;            } ;            // 构造器            // this.setName(name) ;            // this.setPrice(price) ;            // this.setName('JavaScript入门') ;            // this.setPrice(50) ;        } ;        // 构建原型        _book.prototype = {            // 静态公有属性            isJSBook : false,            // 静态公有方法            display : function() {                console.log('执行了静态公有方法:display') ;            }        } ;         // 返回类        return _book ;    })() ;    var book = new Book(03, 'JavaScript设计模式', 399) ;    console.log('book:', book) ;    console.log('book.isJSBook:', book.isJSBook) ;    console.log('book.display:', book.display) ;</script>小结:每个类都有3个部分,第一部分是构造函数内的,这是供实例化对象复制用的,第二部分是构造函数外的,直接通过点语法添加的,这是供类使用的,实例化对象是访问不到的,第三部分是类原型中的,实例化对象可以通过其原型链间接地访问到,也是为供所有实例化对象所共有的。</body>
阅读全文
0 0
原创粉丝点击