构造函数和方法

var Book = function (isbn,title,author) {
this.setIsbn(isbn);
this.setTitle(title);
this.setAuthor(author);
} Book.prototype = {
checkIsbn:function (isbn) {
// todo...
return true;
},
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() {
// todo...
}
} var book = new Book('isbn','Javascript','Jim');
console.log(book.getIsbn());
console.log(book.getTitle());
console.log(book.getAuthor()); var book1 = new Book('isbn');
console.log(book1.getIsbn());
console.log(book1.getTitle());
console.log(book1.getAuthor());

isbn

Javascript

Jim

isbn

No title specified

No author specified

用命名规范区分私有变量

var Book = function (isbn,title,author) {
this.setIsbn(isbn);
this.setTitle(title);
this.setAuthor(author);
} Book.prototype = {
checkIsbn:function (isbn) {
// todo...
return true;
},
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() {
// todo...
}
}

用闭包实现私有方法

var Book = function (newIsbn,newTitle,newAuthor) {
// 私有属性
var isbn,title,author; // 私有方法
function checkIsbn(isbn) {
// todo
return true;
} // 保护方法
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';
}; // 构造函数
this.setIsbn(newIsbn);
this.setTitle(newTitle);
this.setAuthor(newAuthor);
} // 公有方法
Book.prototype = {
display:function () {
// todo...
}
} var book = new Book('isbn','Javascript','Jim');
console.log(book.getIsbn());
console.log(book.getTitle());
console.log(book.getAuthor());

静态方法和属性

var Book = (function () {
// 私有静态属性
var numOfBooks = 0; // 私有静态方法
function checkIsbn(isbn) {
// todo...
return true;
} // 返回构造函数
return function(newIsbn,newTitle,newAuthor) {
// 私有属性
var isbn,title,author; // 特权方法
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';
}; numOfBooks++;
if (numOfBooks > 5) {
throw new Error('Book:只允许创建5个Book对象');
} this.setIsbn(newIsbn);
this.setTitle(newTitle);
this.setAuthor(newAuthor);
}
})(); // 公有静态方法
Book.convertToTitleCase = function (inputString) {
// todo...
return inputString;
} // 公有方法
Book.prototype = {
display:function() {
// todo...
}
}
console.log(Book.convertToTitleCase('test')); // test
var book = new Book('isbn','Javascript','Jim');
console.log(book.getTitle());
var book = new Book('isbn','Javascript','Jim');
console.log(book.getTitle());
var book = new Book('isbn','Javascript','Jim');
console.log(book.getTitle());
var book = new Book('isbn','Javascript','Jim');
console.log(book.getTitle());
var book = new Book('isbn','Javascript','Jim');
console.log(book.getTitle());
var book = new Book('isbn','Javascript','Jim');
console.log(book.getTitle());

test

Javascript

Javascript

Javascript

Javascript

Javascript

...js:46 throw new Error('Book:只允许创建5个Book对象');

Javascript设计模式之创建构造函数和方法的更多相关文章

  1. JavaScript面向对象之创建类和方法

    一,js使用函数来定义类而不是像别的编程语言一样通过关键字class来定义,通过类本身(this)和原型(prototype)来完成面对对象编程! 示例1, //创建ElectronicSignatu ...

  2. JavaScript对象的创建之工厂方法

    通过工厂的方式来创建Person对象,在createPerson中创建一个对象,然后为这个对象设置相应的属性和方法,之后返回这个对象. function createPerson(name, age) ...

  3. JavaScript设计模式学习笔记

    1 JavaScript设计模式深入分析 私有属性和方法:函数有作用域,在函数内用var 关键字声明的变量在外部无法访问,私有属性和方法本质就是你希望在对象外部无法访问的变量. 特权属性和方法:创建属 ...

  4. javascript设计模式系列

    javascript设计模式系列   创建型: 1.抽象工厂模式(Abstract Factory) 2.构建者模式(Builder) 3.工厂方法模式(Factory Method) 4.原型模式( ...

  5. JavaScript设计模式之构造函数模式

    一.构造函数模式概念 构造函数用于创建特定类型的对象——不仅声明了使用过的对象,构造函数还可以接受参数以便第一次创建对象的时候设置对象的成员值.你可以自定义自己的构造函数,然后在里面声明自定义类型对象 ...

  6. javascript --- 设计模式之构造函数模式

    在JavaScript里,构造函数通常是认为用来实现实例的,JavaScript没有类的概念,但是有特殊的构造函数.通过new关键字来调用定义的否早函数,你可以告诉JavaScript你要创建一个新对 ...

  7. 再起航,我的学习笔记之JavaScript设计模式06(工厂方法模式)

    上一次已经给大家介绍了简单工厂模式,相信大家对创建型设计模式有了初步的了解,本次我将给大家介绍的是工厂方法模式. 工厂方法模式 工厂方法模式(Factory Method):通过对产品类的抽象使其创建 ...

  8. javaScript设计模式-创建型设计模式

    我们大家一听到设计模式就感觉设计模式是一个高端的东西,到底什么是设计模式呢?其实设计模式也就是我们的前辈在写代码的时候遇到的问题,提出的解决方案,为了方便人与人之间的交流,取了个名字,叫做设计模式. ...

  9. GOF提出的23种设计模式是哪些 设计模式有创建形、行为形、结构形三种类别 常用的Javascript中常用设计模式的其中17种 详解设计模式六大原则

    20151218mark 延伸扩展: -设计模式在很多语言PHP.JAVA.C#.C++.JS等都有各自的使用,但原理是相同的,比如JS常用的Javascript设计模式 -详解设计模式六大原则 设计 ...

随机推荐

  1. fragment相关

    1.鸿洋大神前两年写的,从最基础的开始详解.对常用的方法都做了精炼的总结,分上下两篇 http://blog.csdn.net/lmj623565791/article/details/3797096 ...

  2. snackbar初体验

    底部弹出的部分就是snackbar的,右侧可添加一个action响应点击事件,遗憾的时貌似只能添加一个 这是华为mate8上面运行出来的效果,颜色之类的都是默认.不同的android版本样式稍有差异 ...

  3. 深入浅出Java垃圾回收机制

    JVM学习笔记 JVM内存管理和JVM垃圾回收 JVM内存组成结构 JVM内存结构由堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: 1)堆 所有通过new创建的对象的内存都在堆中分配,其大小可 ...

  4. [Ogre][地形]OgreTerrain分析以及使用

    Ogre 1.7.2中的地形教程 ○读者可以对照着Ogre1.7.2中的terrain.h源码进行阅读加深理解,蓝色部分均为源码 ○去除了一些具体场景比如添加mesh,设置setAmbientLigh ...

  5. Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏

    Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8091   Accepted: 3918 Descriptio ...

  6. 使用Markdown写文档

    转载于:http://blog.csdn.net/xiahouzuoxin/article/details/19752603 Markdown是一种网络书写语言,其目标是实现易读易写,且兼容HTML语 ...

  7. ocruntime

    原作: http://www.jianshu.com/p/25a319aee33d 三种方法的选择 Runtime提供三种方式来将原来的方法实现代替掉,那该怎样选择它们呢? Method Resolu ...

  8. SqlSever基础 datepart 获取一个日期的月份

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  9. SqlSever基础 ltrim函数 除去字符串左边的空格,右边的中间的不管

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  10. .Net(C#)Parallel"循环"的解释以及与循环的比较

    Parallel 类提供对并行循环和区域的支持. 许多个人计算机和工作站都有两个或四个内核(即 CPU),使多个线程能够同时执行. 在不久的将来,计算机预期会有更多的内核. 为了利用当今和未来的硬件, ...