ch04 函数
///////////// Function Literal
var add = function (a,b){
return a + b;
}
///////////// Invocation
// The Method Invocation Pattern
var myObject = {
value: 0,
increment: function (inc) {
this.value += typeof inc === 'number' ? inc : 1;
}
};
myObject.increment();
document.writeln(myObject.value);
myObject.increment(2);
document.writeln(myObject.value);
// The Function Invocation Pattern
var sum = add(3, 4);
myObject.double = function(){
var that = this; // Workaround
var helper = function(){
that.value = add(that.value, that.value)
};
helper();
};
// Invoke double as a method.
myObject.double();
document.writeln(myObject.value);
// The Constructor Invocation Pattern
var Quo = function(string){
this.status = string;
};
Quo.prototype.get_status = function(){
return this.status;
};
var myQuo = new Quo("confused");
document.writeln(myQuo.get_status());
// The Apply Invocation Pattern
var array = [3, 4];
var sum = add.apply(null, array);
var statusObject = {
status: 'A-OK'
};
var status = Quo.prototype.get_status.apply(statusObject);
//////////////// Arguments
var sum = function(){
var i, sum = 0;
for(i = 0; i < arguments.length; i += 1){
sum += arguments[i];
}
return sum;
};
document.writeln(sum(4, 8, 15, 16, 23, 42));
////////////////// 4.6 Exceptions
var add = function(a, b){
if(typeof a !== 'number' || typeof b !== 'number'){
throw{
name: 'TypeError',
message: 'add needs numbers'
}
}
return a + b;
}
var try_it = function(){
try{
add("seven");
}catch(e){
document.writeln(e.name + ': ' + e.message);
}
}
try_it();
/////////////// 4.7 Argumenting Types
// TODO
Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
Number.method('integer', function(){
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
document.writeln((-10 / 3).integer());
String.method('trim', function(){
return this.replace(/^\s+|\s+$/g, '');
});
document.writeln('"' + " neat ".trim() + '"');
Function.prototype.method = function(name, func){
if(!this.prototype[name]){
this.prototype[name] = func;
}
};
/////////////////// 4.8 Recursion
// hanoi问题,递归求解
var hanoi = function(disc, src, aux, dst){
if(disc > 0){
hanoi(disc - 1, src, dst, aux);
document.writeln('Move disc ' + disc + ' from ' + src + ' to ' + dst);
hanoi(disc - 1, aux, src, dst);
}
}
hanoi(3, 'Src', 'Aux', 'Dst');
// 递归处理浏览器端的文档对象模型
// TODO wark_the_DOM 和 walk 什么关系?
var wark_the_DOM = function walk(node, func){
func(node);
node = node.firstChild;
while(node){
walk(node, func);
node = node.nextSibling;
}
};
var getElementsByAttribute = function(att, value){
var results = [];
wark_the_DOM(document.body, function(node){
var actual = node.nodeType === 1 && node.getAttribute(att);
if(typeof actual === 'string' && // node属性是string,node值等于传入的value,或者value不是string
(actual === value || typeof value !== 'string')){
results.push(node);
}
});
};
// 尾递归求阶乘。js不能做尾递归优化。
var factorial = function factorial(i, a){
a = a || 1;
if(i < 2){
return a;
}
return factorial(i - 1, a * i);
};
document.writeln(factorial(4));
//////////////// Scope
var foo = function(){
var a = 3, b = 5;
var bar = function(){
var b = 7, c = 11;
a += b + c;
};
bar();
};
foo();
////////////// Closure
// 通过调用函数的形式初始化myObject
// 该函数返回一个包含有2个方法的对象,并且这些方法享有继续访问value变量的特权
var myObject = function(){
var value = 0;
return {
increment: function(inc){
value += typeof inc === 'number' ? inc : 1;
},
getVaule: function(){
return value;
}
}
}(); // 立刻执行
// 创建一个名为quo的构造函数. 它构造出带有get_status方法和status私有属性的一个对象。
var quo = function(status){
return {
get_status: function(){
return status;
}
};
};
var myQuo = quo("amazed");
document.writeln(myQuo.get_status());
// 定义一个函数,它设置一个DOM节点为黄色,然后把它变成白色
var fade = function(node){
var level = 1;
var step = function(){
var hex = level.toString(16);
node.style.backgroundColor = '#FFFF' + hex + hex; // TODO 颜色编码
if(level < 15){
level += 1;
setTimeout(step, 100); // setTimeout()方法用于在指定的毫秒数后调用函数或计算表达式
}
};
setTimeout(step, 100);
};
fade(document.body);
// 给一个数组中的节点设置事件处理程序
var add_the_handles = function(nodes){
var i;
for(i = 0; i < nodes.length; i+=1){
node[i].onclick = function(i){
return function(e){
alert(e);
};
}(i);
}
};
add_the_handles(document.body);
///////////// Module
// 寻找字符串中的HTML字符,并替换为它们对应的字符
String.method('deentityify',function(){
// The entity table. Itmaps entity names to characters.
var entity= {
quot: '"',
lt: '<',
gt: '>'
};
// Return the deentityify method.
return function(){
return this.replace(/&([^&;]+);/g, // TODO 正则
function(a, b){
var r = entity[b];
return typeof r === 'string' ? r : a;
})
};
}());
document.writeln('<">'.deentityify());
// 返回一个用来产生唯一字符串的对象
// 唯一字符串由两部分组成:前缀+序列号
// 该对象包括一个设置前缀的方法,一个设置序列号的方法,和一个产生唯一字符串的gensym方法
var serial_maker = function(){
var prefix = '';
var seq = 0;
return {
set_prefix: function(p){
prefix = String(p);
},
set_seq: function(s){
seq = s;
},
gensym: function(){
var result = prefix + seq;
seq += 1;
return result;
}
};
};
var seqer = serial_maker();
seqer.set_prefix('Q');
seqer.set_seq(1000);
var unique = seqer.gensym();
document.writeln(unique);
/////////////// Curry
Function.method('curry', function(){
var slice = Array.prototype.slice;
args = slice.apply(arguments);
that = this;
return function(){
return that.apply(null, args.concat(slice.apply(arguments)));
};
});
var add1 = add.curry(1);
document.writeln(add1(6));
////////////// Memoization
// Fibonacci, beform memoization
var fibonacci = function(n){
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
};
for(var i = 0; i <= 10; i += 1){
document.writeln('//' + i + ': ' + fibonacci(i));
}
// after memoization
var fibonacci2 = function(){
var memo = [0, 1];
var fib = function(n){
var result = memo[n];
if(typeof result !== 'number'){
result = fib(n - 1) + fib(n - 2);
memo[n] = result;
}
return result;
};
return fib;
}();
for(var i = 0; i <= 10; i += 1){
document.writeln('//' + i + ': ' + fibonacci2(i));
}
var memoizer = function(memo, fundamental){
var shell = function(n){
var result = memo[n];
if(typeof result !== 'number'){
result = fundamental(shell, n);
memo[n] = result;
}
return result;
};
return shell;
};
var fabonacci3 = memoizer([0,1], function(shell, n){
return shell(n - 1) + shell(n - 2);
});
var factorial2 = memoizer([1,1], function(shell, n){
return n * shell(n - 1);
});
ch05 继承
5.1 伪类
吐下槽。"the Function constructor that produces the function object runs some code like this:"被译成了“Function构造器产生的函数对象会运行类似这样的一些代码:”。主语都错了。这本书里的错误确实比较多。