2

It took me a while to trace down where the error was coming from but the jQuery get fails (writes 'hmm') with this script. I'm using jQuery 2.1.4

 Object.prototype.funkyThing = function () {
    }

    $.get('testing/plotData.txt', function (data) {
        console.log(data);
    })
    .error(function (jqXHR, textStatus, errorThrown) {
        console.log('hmm');
    });

Yes I know that "funkyThing" doesn't do anything. Just adding this to the Object prototype causes the get to fail with "SYNTAXERROR"

Redu
  • 25,060
  • 6
  • 56
  • 76
MtnManChris
  • 528
  • 5
  • 16
  • Can't reproduce -> https://jsfiddle.net/adeneo/mmgajsvj/2/ – adeneo Oct 11 '16 at 15:35
  • 1
    Well, you've now found out why it's considered bad practice to modify core objects. – VLAZ Oct 11 '16 at 15:35
  • Other then a missing semicolon at the end of the function, that looks fine. I can't see a problem with this. – Dandy Oct 11 '16 at 15:36
  • The problem is changing/adding to native prototypes, it can cause all sorts of problems, but adding something like that should have nothing to do with jQuery's ajax. You're sure you're not hitting the error handler because the request simply fails. – adeneo Oct 11 '16 at 15:37
  • 2
    You should make sure that anything you add to `Object.prototype()` to be non enumerable. Just do your Object and Array.prototype modifications with `Object.defineProperty()` and it won't crash jQuery then. – Redu Oct 11 '16 at 15:37
  • It looked fine. Try to use chrome developer tool to check status code response? – Akivamu Oct 11 '16 at 15:37
  • 1
    A possible duplicate of [Prototyping Object in Javascript breaks jQuery?](http://stackoverflow.com/q/1827458/4543207) – Redu Oct 11 '16 at 15:42
  • @vlaz. JavaScript is still a long ways from being a good language, it is missing things like clone for deep cloning of an object, hence the need to modify object. It does work to create a property but I'm not sure that will work to clone the object and all its underlying objects. – MtnManChris Oct 11 '16 at 15:51
  • @MtnManChris well, whether JavaScript is good or not is going is debatable - some would say "yes" and people who have used JavaScript would say "no". But I don't see how that has anything to do with either deep cloning or adding stiff to the prototype presumable in aid to the last one(?). You have to realise that if you add something to `Object.prototype` then _every single_ object will get that. Even if you do `var obj = {}` it's not an empty object any more, it has that prototype property already. _Any_ single piece of code that expects objects to behave normally, may suddenly break. – VLAZ Oct 11 '16 at 16:05
  • See also [How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop](http://stackoverflow.com/q/13296340/1048572) – Bergi Oct 11 '16 at 16:05

0 Answers0