第四章 函数(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 ...
随机推荐
- 2016HUAS暑假集训训练2 O - Can you find it?
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...
- jquery 温故而知新 Ul 相关的操作
在UL中取得第一级的LI <div id='demo1'> <ul> <li id='1'>1<li> <li id='2'>2< ...
- POM.xml的配置实例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Python起步(2)
单行注释:#多行注释:'''或""" 一条语句写在一行之内,不需要分号分隔两条语句在同一行,中间分号隔开缩进语句块中只有一条语句,可以直接写在“:”之后使用“\”进行续行 ...
- WPF 打开文件、文件夹
打开文件代码: OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = &quo ...
- [原创]CI持续集成系统环境---部署gerrit环境完整记录
开发同事提议在线上部署一套gerrit代码审核环境,不用多说,下面就是自己部署gerrit的操作记录. 提前安装好java环境,mysql环境,nginx环境 测试系统:centos6.5 下载下面三 ...
- JMeter学习-010-JMeter 配置元件实例之 - CSV Data Set Config 参数化配置
众所周知,在进行接口测试的过程中,需要创建不同的场景(不同条件的输入,来验证不同的入参的返回结果).因而,在日常的自动化接口监控或商品监控等线上监控过程中,需要配置大量的入参来监控接口的返回是否正确. ...
- Maven-010-maven 编译报错:Failure to ... in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced.
今晚在编译 maven 项目的时候,命令行报错,出现 Failure to ... in ... 类似错误,详细的错误信息如下所示: [INFO] -------------------------- ...
- asp.net如何在前台利用jquery Ajax调用后台方法
一 :最近因为帮同事开发项目使用到了asp.net,而我又想实现Ajax异步请求....从网上查询了一下资料之后,原来在asp.net中利用Ajax调用后台方法同样很简单,为了便于自己以后查看,特将此 ...
- FPGA学习笔记之格雷码、边沿检测、门控时钟
一.格雷码 格雷码的优点主要是进位时只有一位跳变,误码率低. 1.二进制转格雷码 我们观察下表: 二进制码 格雷码 00 00 01 01 10 11 11 10 二进制码表示为B[],格雷码表示为G ...