设计模式知识连载(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
- 设计模式知识连载(3)---封装_2:闭包
- 设计模式知识连载(2)---封装_1
- 设计模式知识连载(6)---继承_2:创建即继承-构造函数继承
- 设计模式知识连载(4)---封装_3:创建对象的安全模式
- 设计模式知识连载(13)---简单工厂模式:
- 设计模式知识连载(14)---工厂方法模式:
- 设计模式知识连载(15)---抽象工厂模式:
- 设计模式知识连载(16)---建造者模式:
- 设计模式知识连载(17)---原型模式:
- 设计模式知识连载(18)---单例模式:
- 设计模式知识连载(19)---外观模式:
- 设计模式知识连载(20)---适配器模式:
- 设计模式知识连载(21)---代理模式:
- 设计模式知识连载(22)---装饰者模式:
- 设计模式知识连载(23)---桥接模式:
- 设计模式知识连载(24)---组合模式:
- 设计模式知识连载(25)---享元模式:
- 设计模式知识连载(26)---模板方法模式:
- AndroidStudio的JNI编程举例
- 初探React-Native 环境配置(一)
- windows 下Neuxs的安装使用说明
- 移动web输入法自带表情错误处理
- python 模拟登陆csdn
- 设计模式知识连载(3)---封装_2:闭包
- 获取Android手机的分辨率通过ADB命令
- 基于MATLAB的Cholesky分解法(by Violet)
- Activity启动模式
- 常见的网站布局方式---左固定右自适应、左右固定中自适应等
- HDU-1495 非常可乐(BFS)+数论求解
- Hadoop Pig简介、安装、试用
- 再谈 最速下降法/梯度法/Steepest Descent
- Flask