R
raider5
I recently tried out "My Library" (looks nice), and noticed it doesn't
use hasOwnProperty, instead is uses a custom isOwnProperty function.
I've written an alternative hasOwnProperty function to handle a couple
of edge cases isOwnProperty doesn't:
var hasOwnProperty = (function(hop){
return hop ? function(o,p){
return hop.call(o,p);
}:function(o,p){
var proto = o.constructor.prototype, b = true, t;
if(p in proto){
t = proto[p];
b = (proto[p]=1) !== o[p] || (proto[p]=2) !== o[p];
proto[p] = t;
}
return b;
};
}(Object.prototype.hasOwnProperty));
function testIsOwnProperty(){
this.a=1;
}
testIsOwnProperty.prototype.a=1;
testIsOwnProperty.prototype.b=undefined;
var o = new testIsOwnProperty();
for(var p in o){
if(hasOwnProperty(o,p)){
alert(p+':'+o[p]); //a:1
}
}
I'm curious as to what others have implemented for hasOwnProperty
should it not exist? And if there is an alternative method that is
preferable?
use hasOwnProperty, instead is uses a custom isOwnProperty function.
I've written an alternative hasOwnProperty function to handle a couple
of edge cases isOwnProperty doesn't:
var hasOwnProperty = (function(hop){
return hop ? function(o,p){
return hop.call(o,p);
}:function(o,p){
var proto = o.constructor.prototype, b = true, t;
if(p in proto){
t = proto[p];
b = (proto[p]=1) !== o[p] || (proto[p]=2) !== o[p];
proto[p] = t;
}
return b;
};
}(Object.prototype.hasOwnProperty));
function testIsOwnProperty(){
this.a=1;
}
testIsOwnProperty.prototype.a=1;
testIsOwnProperty.prototype.b=undefined;
var o = new testIsOwnProperty();
for(var p in o){
if(hasOwnProperty(o,p)){
alert(p+':'+o[p]); //a:1
}
}
I'm curious as to what others have implemented for hasOwnProperty
should it not exist? And if there is an alternative method that is
preferable?