详解继承
//继承图解 function child() {} function parent() {} var parentObject=new parent(); child.prototype = parentObject; var childObject=new child();
1.继承实现的方法:
1.1 class
1.2 prorortype
2.pseudoclass inheritance
3. new constructor() returns a new object with a linke to constructor.prototype
new omitted ,global object passed by constructor ,return global
no compile time check
no run time check
4. object.p 的过程
object 有属性 p 返回 ,否则 看 object 的构造函数 的 prototype 对象 有无 p 属性 ,没有 就 沿着 prototype 链一直向上找,直到 Object.prototype
4. add members to function's prototype :
add constructors and methods to every object produced ,without the object having to be enlarged to contain them.
5.simulated class inheritance
function child() {}
child.prototype=new parent();
6. singletons -> simple one object literal
7.Functions in javascript :
functions , methods ,constructors ,class,module
8.var singleton = (function () {
var privateField;
//priviledged
function privateMethod() {
}
return {
publicMethod : function () {},
publicField : 1
};
})();
9.parasitic (寄生) Inheritance : (power constructor)
call constructor , argument object returned , return it.
10. Inheritance patterns
prototype inheritance , parastic Inheritance , pseudoclassical Inheritance
11.args= Array.prototype.slice.apply(arguments,[2]);
获得 原参数 第三个参数(包括)后的参数数组
12. loop 变量
for( i ...) {
var div_id=divs[i].id;
divs[i].onmouseover=function () {
show(div_id);
}
}
没有 块作用域 , 结果 所有 div 的 onmouseover处理一样了 ,引用的是同一个 div_id
for( i ...) {
var div_id=divs[i].id;
divs[i].onmouseover=(function (id) {
return function() {
show(div_id);
}
})(div_id);
}
将代码隐藏在 函数作用域中。 或 (Douglas Crockford 没说这个)
for( i ...) {
divs[i].onmouseover=(function () {
var div_id=divs[i].id;
return function() {
show(div_id);
}
})();
}
13 . Javascript : minification and Obfuscation (可能引入bug,不推荐)
14.JSON :eval 前请用 正则表达式验证。