第四章 函数(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 ...
随机推荐
- Kmeans方法
基本Kmeans算法介绍及其实现 http://blog.csdn.net/qll125596718/article/details/8243404/ kmeans++ http://www.52ml ...
- python的内存管理机制
先从较浅的层面来说,Python的内存管理机制可以从三个方面来讲 (1)垃圾回收 (2)引用计数 (3)内存池机制 一.垃圾回收: python不像C++,Java等语言一样,他们可以不用事先声明变量 ...
- ThinkPHP 3.2.3 视图模型的使用
ThinkPHP 3.2.3 试图模型的手册地址是:http://www.kancloud.cn/manual/thinkphp/1781 实例 需求:在博客列表页读取博客的(id.标题.摘要.发布时 ...
- 淘宝天猫网站停止支持IE6、IE7浏览器,你还在用xp吗?
2016年4月14日,是科比正式告别篮球的最后一场球赛.大家都在忙着各种纪念和怀念着看科比打球的青葱岁月.不过已经完美谢幕.而我们今天要说的是微软的IE6.IE7浏览器.淘宝网和天猫商城正式停止支持I ...
- Mac配置
1.显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bool true 2.Mac键盘如何开启键盘上F1 - F12功 ...
- C#编程之委托与事件四(一)【转】
C#编程之委托与事件(一) 本文试图在.net Framework环境下,使用C#语言来描述委托.事件的概貌.希望本文能有助于大家理解委托.事件的概念,理解委托.事件的用途,理解它的C#实现方 ...
- yii框架分页
- Linux配置SSH免密码登陆
配置环境: 两台centos 6.4虚拟机,/etc/hosts配置如下 192.168.63.128 hadoop001 --master192.168.63.131 hadoop002 --sla ...
- 字节流和字符流(PrintStream类和PrintWiter类)
要想输入和输出各种数据类型,通常要打印输入流PrintStream和PrintWriter.其中,PrintStream操作的是字节,PrintWriter操作的是字符. 1:PrintStream类 ...
- react 评论列表插入评论数据 unshift
// unshift 新增数据放到最上面 //插入 回复/发表 评论else if(action.type === INSERT_COMMENT ){ let content = action.tex ...