js深入研究之神奇的匿名函数类生成方式
<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深入研究之神奇的匿名函数类生成方式的更多相关文章
- js闭包中的this(匿名函数中的this指向的是windows)
js闭包中的this(匿名函数中的this指向的是windows) 一.总结 1.普通函数中的this指向的是对象,匿名函数中的this指向的是windows,和全局变量一样 2.让匿名函数中的thi ...
- php匿名函数和闭包函数及use关键字传参及Closure匿名函数类
php闭包函数用use传参有什么意义?答:use引用外层变量,比如全局变量 Closure,匿名函数,是php5.3的时候引入的,又称为Anonymous functions.字面意思也就是没有定义名 ...
- js深入研究之自定义混合Mixin函数
<script type="text/javascript"> /* 增加函数 */ function augment(receivingClass, givingCl ...
- Lambda 闭包 匿名 函数 类
深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法) - _Luc_ - 博客园 https://www.cnblogs.com/figure9/p/java-8 ...
- js匿名函数和闭包总结
js匿名函数和闭包总结 一.总结 一句话总结:匿名函数的最主要作用是创建闭包,闭包就是将函数内部和函数外部连接起来的一座桥梁.内层的函数可以使用外层函数的所有变量,即使外层函数已经执行完毕.闭包可以用 ...
- 匿名函数function前面的! ~等符号作用小解
好久没写博客了,刚过完年,给大家拜个晚年,大家新年快乐! 相信昨晚前端,很多同学应该都见过类似于: !function() {do something...}() ~function(){do som ...
- js深入研究之匿名函数
/* 匿名函数*/ (function() { var foo = 10; var bar = 2; alert(foo * bar);})(); /* 匿名函数,带参数 */ (function(f ...
- JS匿名函数的理解
js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...
- jquery的匿名函数研究
jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...
随机推荐
- [LeetCode] 61. Rotate List 解题思路
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- FreeMarker---数据类型
1.a.ftl 你好,${user},今天你的精神不错! ----------------------------- 测试if语句: <#if user=="老高"> ...
- [Angular 2] Interpolation: check object exists
In Angular2, sometime we use @Output to pass data to parent component, then parent may pass the data ...
- EJB开发第一个无状态会话bean、开发EJBclient
开发第一个无状态会话bean EJB中的三中bean: 会话Bean(Session Bean) 负责与client交互,是编写业务逻辑的地方.在会话bean中能够通过JDBC直接操作数据库.但大多数 ...
- 好的android编码习惯
上一期分享了android内存优化的一些总结,这一期说说我认为的好的编码习惯,然后下一期会做安卓数据库优化的一些总结,逐渐的会将一些性能优化点总结分享出来,肯定是不够全面的希望不足的地方欢迎指出. 良 ...
- 惠普 hpacucli工具使用
命令组成 hpacucli [parameter=value] 查看: 查看所有控制器状态 hpacucli ctrl all show 查看slot 0阵列信息详细状态 (可以查看物理磁盘和逻辑磁 ...
- C#、.NET和ASP.NET三者之间的区别
刚毕业后出去找工作面试的时候就遇到这个问题!.回答不上来.回来网上查的如下: 那么 .NET.C#和ASP.NET这三者之间区别不清楚,到底它们之间有什么联系呢? 1..NET是一个平台,一个抽象的平 ...
- SignalR小计
微软官方例子地址:http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-a ...
- poj1418 Viva Confetti 判断圆是否可见
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Viva Confetti Time Limit: 1000MS Memory ...
- C++程序设计教程学习(1)-第一部分 编程基础
第一章 概述 C++到底难不难学?没有学不会的事情 1.1 程序设计语言 语言 编程语言 人和计算机交流的工具,群体扩大,人人间交流过程描述与信息表达的工具 机器语言,汇编语言,高级语言 1.2 C+ ...