第三章

创建对象的基本模式

方法一:门户大开型
var Book = function(isbn, title, author) {
  if(isbn == undefined ) throw new Error ('Book constructor requires an isbn.');
  this.isbn = isbn;
  this.title = title || 'No title specified';
  this.author = author || 'No author specified';
}
 
Book.prototype.display = function () {
        ...
};
特点:所有属性都是公开的,并且没有有效的验证数据的有效性

方法二:加强对属性的检测
var Book = function(isbn, title, author) {
  if( !this.checkIsbn(isbn)) throw new Error( 'Book: Invalid ISBN.');
  this.isbn = isbn;
  this.title = title || 'No title specified';
  this.author = author || 'No author specified';
}
 
Book.prototype = {
  checkIsbn : function (isbn) {
    if(isbn == undefined || typeof isbn != 'string') {
      return false ;
    }
 
    isbn = isbn.replace(/-/ . ''); // Remove dashes.
    if(isbn.length != 10 && isbn.length != 13) {
      return false ;
    }
 
    var sum = 0;
    if(isbn.length === 10) { // 10 digit ISBN.
      If( !isbn.match(\^\ d{9 }\)) { // Ensure characters 1 through 9 are digits.
        return false ;
      }
 
      for(var i = 0; i < 9; i ++) {
        sum += isbn.charAt(i) * (10 - i);
      }
      var checksum = sum % 11;
      if(checksum === 10) checksum = 'X' ;
      if(isbn.charAt(9 ) != checksum) {
        return false ;
      }
    }
    else { // 13 digit ISBN.
      if(! isbn.match(\ ^\d{12} \)) { // Ensure characters 1 through 12 are digits.
        return false ;
      }
 
      for(var i = 0; i < 12; i ++) {
        sum += isbn.charAt(i) * ((i % 2 === 0) ? 1 : 3);
      }
      var checksum = sum % 10;
      if(isbn.charAt(12 ) != checksum) {
        return false ;
      }
    }
 
    return true ; // All tests passed.
  },
 
  display: function() {
    ...
  }
};
特点:在Book.prototype上添加了一个checkIsbn函数,可以在实例化的时候检查数据的有效性
缺点:还没有解决属性暴露在外面的问题

方法三:添加取值器和赋值器
var Book = function(isbn, title, author) { // implements Publication
  this.setIsbn(isbn);
  this.setTitle(title);
  this.setAuthor(author);
}
 
Book.prototype = {
  checkIsbn : function (isbn) {
    ...
  },
  getIsbn: function() {
    return this .isbn;
  },
  setIsbn: function(isbn) {
    if(! this.checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.' );
    this.isbn = isbn;
  },
 
  getTitle : function () {
    return this .title;
  },
  setTitle : function (title) {
    this.title = title || 'No title specified';
  },
 
  getAuthor : function () {
    return this .author;
  },
  setAuthor : function (author) {
    this.author = author || 'No author specified';
  },
 
  display: function() {
    ...
  }
};
特点:上面方法的一个改进,通过赋值器验证数据的有效性
缺点:赋值器和取值器添加了额外的代码,而且属性还是暴露在外面

方法四:用命名规范区别私用成员
var Book = function(isbn, title, author) { // implements Publication
  this.setIsbn(isbn);
  this.setTitle(title);
  this.setAuthor(author);
}
 
Book.prototype = {
  _checkIsbn : function (isbn) {
    ...
  },
  getIsbn: function() {
    return this ._isbn;
  },
  setIsbn: function(isbn) {
    if(! this._checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.' );
    this._isbn = isbn;
  },
 
  getTitle : function () {
    return this ._title;
  },
  setTitle : function (title) {
    this._title = title || 'No title specified';
  },
 
  getAuthor : function () {
    return this ._author;
  },
  setAuthor : function (author) {
    this._author = author || 'No author specified';
  },
 
  display: function() {
    ...
  }
};
特点:其实也就是把属性的名称下面加了一个下划线"_"。在属性名前加一个下划线是说明这是私有变量的一种约定,使用的人就可以清楚不应该修改这个属性。
缺点:这只是一种约定,可以让别人不误用,但是如果别人有意要修改这私有属性,也是可以的。

方法五:用闭包实现私有成员
var Book = function(newIsbn, newTitle, newAuthor) { // implements Publication
 
  // Private attributes.
  var isbn, title, author;
 
  // Private method.
  function checkIsbn(isbn) {
    ...
  } 
 
  // Privileged methods.
  this.getIsbn = function () {
    return isbn;
  };
  this.setIsbn = function (newIsbn) {
    if(! checkIsbn(newIsbn)) throw new Error ('Book: Invalid ISBN.');
    isbn = newIsbn;
  };
 
  this.getTitle = function () {
    return title;
  };
  this.setTitle = function (newTitle) {
    title = newTitle || 'No title specified';
  };
 
  this.getAuthor = function () {
    return author;
  };
  this.setAuthor = function (newAuthor) {
    author = newAuthor || 'No author specified';
  };
 
  // Constructor code.
  this.setIsbn(newIsbn);
  this.setTitle(newTitle);
  this.setAuthor(newAuthor);
};
 
// Public, non-privileged methods.
Book.prototype = {
  display: function() {
    ...
  }
};
特点:私有变量用var声明而不用this赋给对象,使对象不能直接操作私有变量。此外,用this.method这样的方法定义特权方法(赋值器等),使用于可以通过特权方法操作私有变量,公共方法就定义在Book.prototype中。
缺点:特权方法的增加会耗费更多的内存,不像第一种方法一样把所有方法都放在prototype里面。并且以后的子类都不能访问这这样定义的私有方法

方法五:静态方法和属性
var Book = (function() {
 
  // Private static attributes.
  var numOfBooks = 0 ;
 
  // Private static method.
  function checkIsbn(isbn) {
    ...
  }   
 
  // Return the constructor.
  return function(newIsbn, newTitle, newAuthor) { // implements Publication
 
    // Private attributes.
    var isbn, title, author;
 
    // Privileged methods.
    this.getIsbn = function() {
      return isbn;
    };
    this.setIsbn = function(newIsbn) {
      if(! checkIsbn(newIsbn)) throw new Error ('Book: Invalid ISBN.');
      isbn = newIsbn;
    };
 
    this.getTitle = function() {
      return title;
    };
    this.setTitle = function(newTitle) {
      title = newTitle || 'No title specified';
    };
 
    this.getAuthor = function() {
      return author;
    };
    this.setAuthor = function(newAuthor) {
      author = newAuthor || 'No author specified';
    };
 
    // Constructor code.
    numOfBooks ++; // Keep track of how many Books have been instantiated
                  // with the private static attribute.
    if(numOfBooks > 50) throw new Error( 'Book: Only 50 instances of Book can be '
        + 'created.' );
 
    this.setIsbn(newIsbn);
    this.setTitle(newTitle);
    this.setAuthor(newAuthor);
  }
})();
 
// Public static method.
Book.convertToTitleCase = function (inputString) {
  ...
};
 
// Public, non-privileged methods.
Book.prototype = {
  display: function() {
    ...
  }
};
 
特点:这种方法最大一个特点就是构造函数是通过立即调用函数返回出来的,这样外层的这个立即调用函数就形成了一个闭包,在里面我们就可以声明一些静态私有的变量或者方法了。

方法六:常量
var Class = (function() {
 
  // Private static attributes.
  var constants = {
    UPPER_BOUND : 100 ,
    LOWER_BOUND : -100
  }
 
// Constructor
var ctor = function(constructorArgument) {
     ...
};
 
  // Privileged static method.
  ctor.getConstant(name) {
    return constants[name];
  }
 
  ...
 
  // Return the constructor.
  return ctor;
})();
 
 
/* Usage. */
 
Class.getConstant( 'UPPER_BOUND');
特点:同样上面的那种方法,不同的是只设置一个取值器,不设置赋值器,就可以模仿常量了。

小结:上面所有方法都是返回值都是构造函数来的,如果我们不需要构造函数,就可以利用单体模式,后面章节会说。此外,封装的最好用处就是防止别人修改你的数据,最大的缺点就是麻烦~

一个综合例子:

<html>

<body>
<script>
var Student = (function(){
var _count = ;//私有静态变量
var constants = {//常量
MAX_NUM :
}; function Student(name, age) {
var _name;//私有变量
var _age;//私有变量 var checkAge = function(age) {//私有方法
if(typeof(age) != 'number') {
return false;
} else {
return true;
}
}; this.setName = function(name) {//特权方法
_name = name;
}; this.setAge = function(age) {//特权方法
if(!checkAge(age)) throw new Error("age should be number");
_age = age;
}; this.getName = function() {//特权方法
return _name;
}; this.getAge = function() {//特权方法
return _age;
}; this.setName(name);
this.setAge(age);
_count++;
} Student.getConstant = function(name) {//公共静态方法
return constants[name];
}; Student.getCount = function() {//公共静态方法
return _count;
}; Student.prototype.showName = function() {//公共非特权方法
alert(this.getName());
}; Student.prototype.showAge = function() {//公共非特权方法
alert(this.getAge());
}; return Student;
})(); //使用
var Mark = new Student('Mark', );
var Sally = new Student('Sally', );
</script></body>

 

《JavaScript设计模式》笔记之第三章:封装和信息隐藏的更多相关文章

  1. Javascript设计模式笔记

    Javascript是越来越厉害了,一统前后端开发.于是最近把设计模式又看了一遍,顺便做了个笔记,以方便自己和他人共同学习. 笔记连载详见:http://www.meteorcn.net/wordpr ...

  2. JavaScript设计模式——前奏(封装和信息隐藏)

    前面一篇讲了js设计模式的前奏,包括接口的讲解.. 三:封装和信息隐藏: 信息隐藏用来进行解耦,定义一些私有的数据和方法. 封装是用来实现信息隐藏的技术,通过闭包实现私有数据的定义和使用. 接口在这其 ...

  3. [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步

    本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...

  4. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第三章:变换

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第三章:变换 学习目标 理解如何用矩阵表示线性变换和仿射变换: 学习在 ...

  5. 《JAVASCRIPT高级程序设计》第三章

    <JAVASCRIPT高级程序设计>第三章主要讲述了这门语言的基础概念,内容多而浅,通过思维导图可以帮助我们很好的理清脉络. js函数使用function关键字来声明,以下是一个简单的例子 ...

  6. javascript面向对象精要第三章对象整理精要

    什么是对象的数据属性?什么是对象的访问器属性?[put]方法是默认创建数据属性的,访 问器属性不包含值而是定义了一个单属性被读取时调用的函数(getter)和当一个属性被写入时 调用的函数(sette ...

  7. 如何封装JS ----》JS设计模式《------ 封装与信息隐藏

    1. 封装与 信息隐藏之间的关系 实质是同一个概念的两种表达,信息隐藏式目的,二封装是借以达到目的的技术方法.封装是对象内部的数据表现形式和实现细节,要想访问封装过额对象中的数据,只有使用自己定义的操 ...

  8. JS设计模式——3.封装与信息隐藏

    封装.信息隐藏与接口的关系 信息隐藏是目的,封装是手段. 接口提供了一份记载着可供公共访问的方法的契约.它定义了两个对象间可以具有的关系.只要接口不变,这个关系的双方都是可以替换的. 一个理想的软件系 ...

  9. JavaScript DOM编程艺术-学习笔记(第三章、第四章)

    第三章: 1.js的对象分为三种:①用户自定义对象 ② 内建对象(js提供的对象) ③宿主对象(js寄宿的环境-浏览器,提供的对象) 2.文档是由节点组成的集合,即dom树,html元素是根元素,是唯 ...

随机推荐

  1. Yii的缓存机制之数据缓存

    具体说法就是可以缓存变量信息. 设置:Yii::app()->cache->set(名字, 值, 过期时间): 使用:Yii::app()->cache->get(名字); 删 ...

  2. 提高scroll性能

    在DevTools中开始渲染,向下滑动一点点滚动条,然后停止滚动. 在结果中,注意frames总是在30ftps线上面,甚至都木有很接近69ftps线的(事实上帧执行的太缓慢以致于60ftps线在图上 ...

  3. 【错误信息】springMVC No mapping found for HTTP request with URI

    出现这个问题的原因是在web.xml中配置错了:

  4. ACM2016级新生第三周训练赛

    本次是弱校题解-比赛链接 备用链接 题目还是比较基础,比较简单.认真补题,学会学习. A -人见人爱A^B 题解: 求 A的B次方,我们可以用循环进行累乘操作,进而计算出次方.因为题目要求只需要求出最 ...

  5. Asterisk 拨号方案中截取字符串

    在dialplan中要截取字符串可用如下几种方式: 1.转到agi中由php进行处理,这种方法比较简单,在这里不再缀述 2.在dialplan中利用内置的方法进行截取 如 [test] exten = ...

  6. 「SDFZ听课笔记」二分图&&网络流

    二分图? 不存在奇环(长度为奇数的环)的图 节点能黑白染色,使得不存在同色图相连的图 这两个定义是等价哒. 直观而言,就是这样的图: 二分图有一些神奇的性质,让一些在一般图上复杂度飞天的问题可以在正常 ...

  7. poj2226Muddy Fields——二分图匹配

    题目:http://poj.org/problem?id=2226 把行连通块作为左部点,列连通块作为右部点,行列连通块有相交的格子就连边: 则问题转化为求最小点覆盖,即最大匹配. 代码如下: #in ...

  8. vue-router 安装

    如果在一个模块化工程中使用它,需要通过Vue.use() 明确的安装路由功能,如果使用全局的script标签,则不需要手动安装. Vue Router是Vue.js官方的路由管理器.它和Vue.js的 ...

  9. javamail - 发件、收件(SSL连接)

    需要的包:mail.jar  jsse.jar(说明:jsse.jar是jre自带的,如果jdk1.5及以下需要Java Activation Framework框架的activation.jar) ...

  10. [WIP]webpack入门

    创建: 2019/04/09  安装 npm install --save-dev webpack # 最新版 npm install --save-dev webpack@<version&g ...