1

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?

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but also because the "other library" has not taken precautions against inherited properties when using for..in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.