第四章 函数(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 ...
随机推荐
- 【HDU4612】 双连通分量求桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 题目大意:给你一个无向图,问你加一条边后最少还剩下多少多少割边. 解题思路:好水的一道模板题.先 ...
- centos6.6 LVS+keepalived
之前有写过keepalived+mysql 和lvsDR模式的分析篇.然而LVS没有写高冗余.今天来写一篇LVS+keepalived的 LVSDR只负责转发,LVS也没有nginx后端检查功能,所 ...
- NEC学习 ---- 模块 -多行式面包屑导航
如上面形式面包屑的写法: HTML如下, <div class="m-crumb"> <ul class="f-cb"> <li& ...
- Web标准和搜索引擎优化技术
1.Web标准不是某一个标准,而是一系列标准的集合.出来网页内容之外,网页主要由三部分组成:结构(Structure).表现(Presenttation)和行为(Behavior).对应的标准也分三方 ...
- SharedPreferences第一次使用后HashMap将常驻内存
今天使用SharedPreferences的时候突然想到了这个问题,因为我们要存储应用级别的上下文信息,包括用户信息等一系列信息:这个时候使用getSharedPreferences是否合适呢! 其实 ...
- schtasks在win7下提示错误:无法加载列资源
转自: http://blog.chinaunix.net/uid-24946452-id-2887851.html 查看cmd 编码 chcp 如使用 936中文GBK编码的话 schtasks.e ...
- robots
User-agent: Baiduspider Disallow: /w? Allow: / User-agent: Googlebot Allow: / User-agent: Googlebot- ...
- angularJs内置指令63个
- 显示HTML文本
+ (NSAttributedString*)getAttributedStringFromHtmlString:(NSString*)htmlString{ return [[NSAttribute ...
- Git commit 常见用法
Git commit git commit 主要是将用户通过git add命令添加到暂存区里的改动给提交到本地的版本库,关于版本库的构成可以查看我先前的笔记. 每次提交我们都会在本地版本库生成 ...