第四章 函数(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 ...
随机推荐
- Lua迭代器和泛型for
1.迭代器与closure 在lua中,迭代器通常为函数,每调用一次函数,会返回集合中的下一个元素.每个迭代器在成功调用的时候,都需要保存一些状态,closure(闭包)完美为迭代器运用而生. fun ...
- linux中添加ftp用户,并设置相应的权限
在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...
- css属性编写顺序+mysql基本操作+html细节(个人笔记)
css属性编写顺序: 影响文档流的属性(比如:display, position, float, clear, visibility, table-layout等) 自身盒模型的属性(比如:width ...
- 获取 苹果UDID 序列号
UDID是什么? UDID 是由子母和数字组成的40个字符串的序号,用来区别每一个唯一的iOS设备,包括 iPhones, iPads, 以及 iPod touches,这些编码看起来是随机的,实际上 ...
- Run P4 without P4factory - A Simple Example In Tutorials. -2 附 simple_router源码
/* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (th ...
- mysql组合索引顺序参考
问题背景 : 当我们需要创建一个组合索引, 索引的顺序对于效率影响很大, 怎么确定索引的顺序; 解决方法 : 我们应该依据字段的全局基数和选择性, 而不是字段的某个具体的值来确定; 表结构 : dc ...
- BigPipe学习研究
BigPipe学习研究 from: http://www.searchtb.com/2011/04/an-introduction-to-bigpipe.html 1. 技术背景 FaceBook ...
- c# 文件遍历
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName); //遍历文件夹 foreach(DirectoryInfo NextFolder ...
- Nginx配置proxy_pass转发的/路径问题
Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...
- Xamarin Android 真机调试时闪退
方法1:引起此问题的原因一般是因为 Mono Shared Runtime 在手机上没有运行,这个程序相当于.net运行时,没有运行的话用C#开发的程序自然无法运行. 解决方法是将此程序设置为自动运行 ...