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的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...
随机推荐
- 【KMP】剪花布条
KMP算法 又水了一题.算是巩固复习吧. Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽 ...
- SqlServer 自动备份、自动删除7天前备份
-----sqlserver 数据异地备份 默认删除 七天前的数据 -----该代码可配置成SQLServer作业里做调度,或者配置成任务计划进行执行 ----挂载异地盘符 exec master.. ...
- [RxJS] Creation operator: of()
RxJS is a lot about the so-called "operators". We will learn most of the important operato ...
- android 判断是否有sim卡及运营商
判断是否有sim卡的方法: int absent = TelephonyManager.SIM_STATE_ABSENT; if (1 == absent) { Log.d(TAG,"请 ...
- Redmine backlogs 升级
刚装完1.0.3两天,1.0.4发布了,乘项目还没有开始,赶快升级.升级过程 1.设置环境变量: RAILS_ENV=production export RAILS_ENV 2. 获取最新代码: cd ...
- 解决Android SDK Manager下载太慢问题(转)
1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...
- NHibernate+NUnit (VS2012+SQL Server2008) (转)
源博客:http://blog.csdn.net/jiajiayouba/article/details/9877875 首先,你要明白,NHibernate在.NET中的作用和Hibernate在J ...
- (转)asp.net动态设置标题title 关键字keywords 描述descrtptions
方法一 if (!IsPostBack){//Page title网页标题Page.Title = “我的网站标题”;//须将网页head标签设成服务器控件模式,即<head runat=&qu ...
- GridView 和 Access数据库实现数据绑定(asp.net)
前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="主页.aspx.cs ...
- 业余编程 SQL 编程学习——1 (SQL Server 2008 R2)
1.建立test数据库: 2.创建test1数据表: test1表属性如下: 其中,ID字段设置为标识增量,增量种子为1: 这个实例是实现从第一行数据开始,将每一行最后一个字段值加下一行第二个字段值再 ...