Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Can you post a demo to reproduce the problem? –  elclanrs Nov 25 '13 at 6:12
3  
Are you sure you want to add to the Object prototype?? This will add the getByPath method to all objects –  jasonscript Nov 25 '13 at 6:15
    
Yeah, it's not recommended (or even efficient) to do this. –  m59 Nov 25 '13 at 6:17
    
Do you know any right way to solve this? –  user889349 Nov 25 '13 at 6:19
    
I'll guess that "starts with this line" ends in ());. –  RobG Nov 25 '13 at 6:27

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
1  
+1, but also because the "other library" has not taken precautions against inherited properties when using for..in. –  RobG Nov 25 '13 at 6:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.