<script type="text/javascript">
var Book = (function() { // 私有静态属性
var numOfBooks = ; // 私有静态方法
function checkIsbn(isbn) {
if(isbn == undefined || typeof isbn != 'string') {
return false;
}
return true;
} // 返回构造函数
return function(newIsbn, newTitle, newAuthor) { // implements Publication // 私有属性
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++; // Keep track of how many Books have been instantiated
// with the private static attribute.
if(numOfBooks > ) throw new Error('Book: Only 5 instances of Book can be '
+ 'created.'); this.setIsbn(newIsbn);
this.setTitle(newTitle);
this.setAuthor(newAuthor);
}
})(); // 公有静态方法
Book.convertToTitleCase = function(inputString) {
alert('convertToTitleCase');
}; // 公有非特权方法
Book.prototype = {
display: function() {
alert("isbn:"+this.getIsbn()+" title:"+this.getTitle()+" author:"+this.getAuthor());
}
};
//var theHobbit = new Book(123, '', 'J. R. R. Tolkein'); // 非字符串抛出异常
var theHobbit = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
theHobbit.display();
//theHobbit.convertToTitleCase(); // Uncaught TypeError: Object #<Object> has no method 'convertToTitleCase'
Book.convertToTitleCase(); // 输出convertToTitleCase var theHobbit2 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
theHobbit2.display(); var theHobbit3 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
theHobbit3.display(); var theHobbit4 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
theHobbit4.display(); var theHobbit5 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
theHobbit5.display(); var theHobbit6 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein');
theHobbit6.display(); // Uncaught Error: Book: Only 5 instances of Book can be created. </script>

这里已经把js出神入化了,佩服到极致,代码清晰简洁,美观,注释恰到好处。

js深入研究之神奇的匿名函数类生成方式的更多相关文章

  1. js闭包中的this(匿名函数中的this指向的是windows)

    js闭包中的this(匿名函数中的this指向的是windows) 一.总结 1.普通函数中的this指向的是对象,匿名函数中的this指向的是windows,和全局变量一样 2.让匿名函数中的thi ...

  2. php匿名函数和闭包函数及use关键字传参及Closure匿名函数类

    php闭包函数用use传参有什么意义?答:use引用外层变量,比如全局变量 Closure,匿名函数,是php5.3的时候引入的,又称为Anonymous functions.字面意思也就是没有定义名 ...

  3. js深入研究之自定义混合Mixin函数

    <script type="text/javascript"> /* 增加函数 */ function augment(receivingClass, givingCl ...

  4. Lambda 闭包 匿名 函数 类

    深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法) - _Luc_ - 博客园 https://www.cnblogs.com/figure9/p/java-8 ...

  5. js匿名函数和闭包总结

    js匿名函数和闭包总结 一.总结 一句话总结:匿名函数的最主要作用是创建闭包,闭包就是将函数内部和函数外部连接起来的一座桥梁.内层的函数可以使用外层函数的所有变量,即使外层函数已经执行完毕.闭包可以用 ...

  6. 匿名函数function前面的! ~等符号作用小解

    好久没写博客了,刚过完年,给大家拜个晚年,大家新年快乐! 相信昨晚前端,很多同学应该都见过类似于: !function() {do something...}() ~function(){do som ...

  7. js深入研究之匿名函数

    /* 匿名函数*/ (function() { var foo = 10; var bar = 2; alert(foo * bar);})(); /* 匿名函数,带参数 */ (function(f ...

  8. JS匿名函数的理解

    js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...

  9. jquery的匿名函数研究

    jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...

随机推荐

  1. 公告:本博客搬迁到:http://www.courtiercai.com/

    公告:       您好,本人意见本博客搬迁到:http://www.courtiercai.com/.

  2. navicat 随笔提示的快捷键

    1.ctrl+q 打开查询窗口2.ctrl+/ 注释sql语句3.ctrl+shift +/ 解除注释4.ctrl+r 运行查询窗口的sql语句5.ctrl+shift+r 只运行选中的sql语句6. ...

  3. iOS- UITableView could not hold the selected row after reload

    I like stackoverflow.. Answer : http://stackoverflow.com/questions/6649202/uitableview-doesnt-keep-r ...

  4. zookeeper[6] zookeeper FAQ(转)

    转自:http://www.cnblogs.com/zhengran/p/4601855.html 1. 如何处理CONNECTION_LOSS?在Zookeeper中,服务器和客户端之间维持一个长连 ...

  5. MyBatis第一个项目示例

    1.创建一个Java project,JikeBook 2.添加项目所需的依赖包 如 mybatis-3.2.8.jar,是实现mybatis功能所必需的依赖包 mysql-connector-jav ...

  6. 你需要知道的九大排序算法【Python实现】之基数排序

    八.基数排序 基本思想:基数排序(radix sort)属于"分配式排序"(distribution sort),又称"桶子法"(bucket sort)或bi ...

  7. 学习 Netty 3.x

    study link: http://netty.io/3.6/guide/#architecture 应用场景: Chat server that requires persistent conne ...

  8. 【转载】Python编程中常用的12种基础知识总结

    Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进 ...

  9. Find命令简介

    Find命令主要用于目标的搜索,尽量做到少使用,因为find会消耗大量的系统资源. 使用该命令时,需要避开服务器运行高峰期,最好在指定的小范围内进行搜索,不要轻易使用全盘搜索. Find命令常用的参数 ...

  10. mybatis之mapper.xml分析

    select: id:方法名,在同一个mapper.xml中,要保持唯一 parameterType:指定输入的参数类型,不是必须的,如果不指定,mybatis会自动识别(推荐指定). resultT ...