第四章 函数(JavaScript:语言精粹)
// 4.2 code 1
var add = function (a, b) {
return a + b;
};
// 4.4 code 2
var myObj = {
value: 0,
increment: function (inc) {
this.value += typeof inc === 'number' ? inc : 1;
}
}; myObj.increment();
myObj.value //
myObj.increment(2);
myObj.value //
// 4.5 code 3
var sum = add(3, 4); //
// 4.5 code 4
myObj.double = function () { var helper = function () {
this // Oops, window Object!
}
}
// 4.5 code 5
myObj.double = function () {
var that = this; var helper = function () {
that.value = add(that.value, that.value);
} helper(); // Function Invocation
} myObj.double(); // Method Invocation
myObj.value //
// 4.6 code 6
var Quo = function (str) {
this.status = str;
} Quo.prototype.get_status = function () {
return this.status;
} var quo1 = new Quo('online');
quo1.get_status() // "online"
// 4.7 code 7
var arr = [3, 4];
var sum = add.apply(null, arr);
sum // var statusObj = {
status: 'offline'
};
var status = Quo.prototype.get_status.apply(statusObj);
status // "offline"
// 4.8 code 8
var sum = function () {
var i, sum = 0; // this "sum" is different from the outer "sum"
for (i = 0; i < arguments.length; i+= 1) {
sum += arguments[i];
}
return sum;
} sum(2, 4, 6, 8, 10); //
// 4.10 code 9
var add = function (a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw {
name: 'TypeError',
message: '相加需要数字,OK?'
};
}
return a + b;
}
// 4.10 code 10
var try_it = function () {
try {
add('Give me Five!');
} catch (ex) {
document.writeln(ex.name + ':' + ex.message); // "TypeError:相加需要数字,OK?"
}
} try_it();
// 4.11 code 11
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
}
// 4.11 code 12
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
}); (-2.4).integer(); // -2
(-2.6).integer(); // -2
(2.4).integer(); //
(2.6).integer(); //
// 4.11 code 13
String.method('trim', function () {
return this.replace(/^\s+|\s+$/g, '');
}); ' so~ hot! '.trim(); // "so~ hot!"
// 4.11 code 14
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
}
return this;
}
// 4.12 code 15
var hanoi = function (disc, src, aux, dst) {
if (disc > 0) {
hanoi(disc - 1, src, dst, aux);
document.writeln(disc + ' 从 ' + src + ' 转移到了 ' + dst + '<br>');
hanoi(disc - 1, aux, src, dst);
}
} hanoi(3, 'SRC', 'AUX', 'DST');
1 从 SRC 转移到了 DST
2 从 SRC 转移到了 AUX
1 从 DST 转移到了 AUX
3 从 SRC 转移到了 DST
1 从 AUX 转移到了 SRC
2 从 AUX 转移到了 DST
1 从 SRC 转移到了 DST

// 4.12 code 16
var walk_the_DOM = function walk(node, func) {
func(node);
node = node.firstChild;
while (node) {
walk(node, func);
node = node.nextSibling;
}
}
// 4.12 code 17
var getElementsByAttribute = function (att, value) {
var result = []; walk_the_DOM(document.body, function (node) {
var actual = node.nodeType === 1 && node.getAttribute(att);
if (typeof actual === 'string' && (actual === value || typeof value !== 'string')) {
result.push(node);
}
}); return result;
}
// 4.12 code 18
var factorial = function factorial(i, a) {
a = a || 1;
if (i < 2) {
return a;
}
return factorial(i - 1, a * i);
} factorial(4); //
// 4.13 code 19
var foo = function () {
var a = 3, b = 5; var bar = function () {
var b = 7, c = 11; // now. a = 3, b = 7, c = 11 a += b + c; // now. a = 21, b = 7, c = 11
} // now. a = 3, b = 5, c is undefined bar(); // now. a = 21, b = 5
}
第四章 函数(JavaScript:语言精粹)的更多相关文章
- 第三章 对象(JavaScript:语言精粹)
对象是属性的容器,其中每个属性都有名字和值. 3.0. 概览:对象字面量 | 检索 | 更新 | 引用 | 原型 | 反射 | 枚举 | 删除 | 减少全局变量污染 3.1. 对象字面量 ...
- 《JavaScript语言精粹》之函数化
写在前面 看到好多书评和读书笔记都说<JavaScript语言精粹>字字珠玑,名不虚传..当然,要看得懂才行 其实个人认为函数化部分不是很好,举的例子不是十分恰当,之前看不懂是因为被成功误 ...
- JavaScript中对象与函数的某些事[JavaScript语言精粹-N1]
今天在读<JavaScript语言精粹>的时候,关于函数的一个部分,始终觉得有点难以理解,代码如下: 1: var obj = (function(){ 2: var value = 0; ...
- JavaScript语言精粹 笔记02 函数
函数函数对象函数字面量调用参数返回异常给类型增加方法递归作用域闭包回调模块级联套用记忆 函数 1 函数对象 在JS中函数就是对象.对象是“名/值”对的集合并拥有一个连接到原型对象的隐藏连接.对象字 ...
- JavaScript语言精粹(读书笔记)
第一章 精华 1,JavaScript的函数(主要)基于词法作用域(lexical scoping)的顶级对象.强类型语言允许编译器在编译时检测错误,但弱类型很自由,无需建立复杂的类层次,不用做强制造 ...
- javascript语言精粹
内容选自:<javascript语言精粹> 1.6种值会为假(==false),分别是false,null,undefined,' ',0,NaN 2.typeof有6种值,分别是'num ...
- 《JavaScript语言精粹》学习笔记
一.in的用法 for...in 枚举一个对象的所有可枚举属性 检测DOM/BOM属性 if ("onclick" in elem) { // 元素支持onclick } if ( ...
- 《JavaScript语言精粹》【PDF】下载
<JavaScript语言精粹>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382204 内容简介 javascript曾是&q ...
- JavaScript 语言精粹笔记3
方法 毒瘤 糟粕 记录一下阅读蝴蝶书的笔记,本篇为书中最后一部分:方法.代码风格.优美的特性.毒瘤.糟粕等. 方法 这一章主要介绍了一些方法集.这里写几个我不太熟悉的方法和要点吧. array.joi ...
随机推荐
- 【HDU4419 Colourful Rectangle】 线段树面积并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:给你n个矩形,每个矩形都有一种颜色,矩形覆盖会出现另外一种颜色,问你所有矩形中不同的颜 ...
- swift 2.x学习笔记(二)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0. ...
- Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t
Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...
- embedded tomcat context.xml
在网络下载相关的embedded tomcat jar.也可直接在maven中检索. 在main方法中,输入以下代码: //新建tomcat实例 Tomcat tomcat = new Tomcat( ...
- 生产环境下的mysql主从复制
一.主mysql配置:1.配置my.cnf[mysqld]server-id = 10 #服务器标示log-bin= mysql-bin #二进制日志binlog-do-db=mydb #需要同步的数 ...
- FreeMarker的教程
copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...
- Win8.1密钥
Win8.1 在线永久激活密钥一枚! 78BHN-M3KRH-PCP9W-HQJYR-Q9KHD [剩余次数:7K多+] 继续增加 [Key]:HPCJW-VGYW4-CR7W2-JG6Q7-K4Q ...
- iOS 1-2年经验面试参考题
Model层: 数据持久化存储方案有哪些? 沙盒的目录结构是怎样的?各自一般用于什么场合? SQL语句问题:inner join.left join.right join的区别是什么? SQLite的 ...
- 一个编程小白,如何入门APP软件开发领域?
近些年,互联网创业火得不得了!一时间,满世界都在招做App软件开发的专业人员.从大众角度来看,学编程,写代码,是一件非常困难的事情.但是,App开发人员的工资那么诱人,让很多小白也跃跃欲试想学一下.那 ...
- MVC 区域功能
因为MVC项目是要求都放在固定的文件夹,所以,当项目大的时候,会很不方便管理,所以微软引入的区域的功能 使用方法: 在项目上右击--添加--区域 就会出现Areas的文件夹,里面就是子MVC 渲染: ...