javascript2

9.6非继承的扩展
9-5带有通用的供借用的方法的混入类
instanceof则为判断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例;
返回boolean类型
语法为 o instanceof A

prototype 属性使您有能力向对象添加属性和方法

从一个类借用方法供另一个类使用
fuction borrowMethods(borrowFrom,addTo){
var from = borrowFrom.prototype;
var to= addTo.prototype;
for(m in from){
if(typeof from[m] !="function")continue;
to[m] = from[m];
}
}

GenericToString.prototype.toString = function(){
var props = [];
for(var name in this){
if(!this.hasOwnPrototype(name)) continue;
var value = this[name];
var s = name+":";
switch(typeof vale){
case 'function':
s += "function";
break;
case 'object':
if(value instanceof Array) s +="array"
else s +=value.toString(); s += "function";
break;
defaule :
s +=String(value);
break;

}
props.push(s);
}
return : "{"+props.join("'")+"}";
}

GenericEquals.prototype.equals = function(that){
if(this==that) return true;
var propsInThat = 0;
for(var name in that){
propsInThat++;
if(this[name] !== that[name]) return false;
}
var propsInThis = 0;
for(var name in this) propsInThis++;
if(propsInThis != propsInThat) return false;
return true;
}

9.7确定对象类型
typeof的主要用途还是从对象中区分出来基本类型。
typeof null是object
typeof undefined 是 undefined;
任何 数组的类型都是object
任何函数的类型都是 "function"
9.7.1 instanceof 和构造方法
instanceof 作用于对象

instanceof 对函数也有效

如果要测试对象是否是一个具体的类的一个实例,以及是否不是某个子类的实例,
可以查看对象的constructor 属性。
var d = new Date();
var isobject = d instanceof Object;
var realobject = d.constructor==Object;
9.7.2 用Object.toString()测试对象的类型

9-6扩展的typeof 测试
function getType(x){
if(x==null) return "null";
var t = typeof x;
if(t !=="object") return t ;
var c = Object.proptotype.toString.apply(x);
c = c.substring(8,c.length-1);
if(c != "Object") return c;
if(x.constructor == Object) return c;
if("classname" in x.constructor.prototype.classname &&
typeof x.constructor.prototype.classname=="string")
return x.constructor.prototype.classname;
return "<unkuown type>";
}

9.7.3 鸭子类型识别
function boeeows(o,c){
if(o instanceof c) return true;
if(c==Array || c==Boolean|| c==Date||
c==Error|| c==Function|| c==Number|| c==RegExp|| c==String)
return undefined;
if(typeof o=="function") o.proptotype;
var proto = c.prototype;
for(var p in proto){
if(typeof proto[p] !=""function) continue;
if(o[p]!=proto[p]) return false;
}
return true;
}

function provodes(o,c){
if(o instanceof c) return true;
if(c==Array || c==Boolean|| c==Date||
c==Error|| c==Function|| c==Number|| c==RegExp|| c==String)
return undefined;
if(typeof o=="function") o.proptotype;
var proto = c.prototype;
for(var p in proto){
if(typeof proto[p] !=""function) continue;
if(!(p in o)) return false;
if(typeof o[p]!="function") return false;
if(o[p].length != proto[p].length) return false;
}
return true;
}

9.8例子 : 一个defineClass()工具方法
9-10 用来定义类的一个工具函数
200-204
function defineClass(data){

}

第10章模块和名字空间
JavaScript模块的第一条规则:一个模块不应该为全局名字空间添加多于
一条的标记。下面是对这条规则的两条很好的常识性的附则:

如果一个模块为全局名字空间添加一条标记,其文档应该清楚地描述改标记是什么。

如果一个模块为全局名字空间添加一条标记,改标记的名字和载入改模块的文件的
名字中之间应该有一种清楚的关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值