Reputation: 3392
I need new method for the object. And I'm trying to create it:
Object.prototype.getByPath = function (path, other) {
for (var i=0, obj=this, path = path.split('.'), len=path.length; i<len; i++) {
obj = obj[path[i]];
}
return (typeof obj === "undefined" || obj == "") ? other : obj;
}
But this code return an error (Conflict with another js file!):
Uncaught TypeError: Object function (path, other) {
Another js file start with this line:
(function(){function d(a,b){
try {
for (var c in b)
Object.defineProperty(a.prototype, c, {value:b[c],enumerable:!1})
} catch(d) {
a.prototype = b
}
}());
How can I solve this error?
Upvotes: 1
Views: 758
Reputation: 21366
Conflict with another js file!
Yes it happens because it is adding the new method to all the objects , Instead try to make your own base object for all your clients side javascript objects,
var yourBaseObj={
getByPath :function (path, other) {
for (var i=0, obj=this, path = path.split('.'), len=path.length; i<len; i++) {
obj = obj[path[i]];
}
return (typeof obj === "undefined" || obj == "") ? other : obj;
}
}
And then you it for other objects ,
function YourNewObject(){
}
YourNewObject.prototype=yourBaseObj
Upvotes: 2