构造函数和方法

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. 用C#.NET编写软件注册机

    验证注册码是保护软件产品产权的常用手段.一般过程如下, 1.  软件发行者收集用户特有的信息: 2.  根据用户特有的信息,使用注册机生成注册码并把注册码发给客户: 3.  向软件导入注册码,由软件自 ...

  2. 《深入浅出MFC:》

    callback函数必须是static的,才能去除隐藏的this指针.

  3. 【转】成功在AMD主机上用虚拟机安装原版雪豹

    转载地址:http://www.jzh.me/archives/205.html/comment-page-1 一直都很想安装苹果的系统,当雪豹出来的时候就更加想了,但是自己的机器是AMD的,而且还是 ...

  4. Spark函数

    这张图不错!

  5. TelephonyManager类:Android手机及Sim卡状态的获取

    TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app;import android.ap ...

  6. BLOB:大数据,大对象,在数据库中用来存储超长文本的数据,例如图片等

    将一张图片存储在mysql中,并读取出来(BLOB数据:插入BLOB类型的数据必须使用PreparedStatement,因为插入BLOB类型的数据无法使用字符串拼写): -------------- ...

  7. Java学习之路(七)

    1:什么是异常?  中断了正常指令流的事件. 异常是一个对象 ,在出现异常时,虚拟机会生成一个异常对象 生成对象的类是由 JDK 提供的

  8. 使用jquery再次封装ajax

    $.fn.ajaxSend = function (type, url, postdata, onSuccess) { $.ajax({ async: false, url: url, type: t ...

  9. word2007如何进行批注

    在正常的办公或者学校撰写论文,请别人进行提出修改意见是不可避免的,在word2007中提供了批注修改模式,十分方便,给撰写文档和批阅文档的人带来了极大的方便.本节介绍如何在word2007中进行批注及 ...

  10. Linux源代码分析工具链

    前言 看源代码是一个程序员必须经历的事情,也是可以提升能力的一个捷径.个人认为: 要完全掌握一个软件的方法只有阅读源码. 在Windows下有sourceinsight这个源码阅读软件(虽然我没用过, ...