IE中如果一个对象中包含以下特殊的属性
var protoprops = [
'toString',
'valueOf',
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerabel',
'toLocaleString'
];
那么这些属性在for…in…中不会被枚举
for( var p in {string: null}) {
alert(1);
}
以上代码在六年前的IE大部分版本中是不会弹出 1 的
为了更好的兼容
var extend = (function (){
var p,
for( p in { toString: null} ) {
return function (o) {
var i,source;
for(i = 0; i < arguments.length; i++) {
source = arguments[i];
for( var prop in arguments[i]) {
o[prop] = source[prop];
}
}
return o;
}
}
var protoprops = [
'toString',
'valueOf',
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerabel',
'toLocaleString'
];
return function (o) {
var j, source,, prop, i;
for( j = 0 ; j < arguments.length; j++) {
source = arguments[i];
for( prop in source) o[prop] = source[prop];
for( i = 0; i < props.length; i++){
if(souce.hasOwnProperty(props[i]))
{ prop = props[i];
o[prop] = source[prop]
}
}
return o;
}
})()