10

I'm developing a UserScript and i thought it would be more time saving to create 2 Prototype functions for Object.

Object.prototype.Count = function() {
    var size = 0, key;
    for (key in this) {
        if (this.hasOwnProperty(key)) {
             size++;
        }
    }
    return size;
};
Object.prototype.GetEntry = function(index) {
    var size = 0, key;
    for (key in this) {
        if (this.hasOwnProperty(key)) {
            if (size == index)
                return this[key];
             size++;
        }
    }
    return null;
};

This 2 Functions are Working perfectly fine on my Debug Console as i Type them, and i use them, however when i'm running my Script, it get some weird errors flooding my console.

Uncaught TypeError: U[a].exec is not a function
Uncaught TypeError: (ec[b] || []).concat is not a function
Uncaught TypeError: X[g].exec is not a function
Uncaught TypeError: (Qn[t] || []).concat is not a function

And more like that, making Site's JavaScript not working.

Without These Functions my Script works like charm. I also have on more Prototype on String, but this is working just fine

String.prototype.between = function(prefix, suffix) {
    s = this;
    var i = s.indexOf(prefix);
    if (i >= 0) {
        s = s.substring(i + prefix.length);
    }
    else {
        return '';
    }
    if (suffix) {
        i = s.indexOf(suffix);
        if (i >= 0) {
            s = s.substring(0, i);
        }
        else {
            return '';
        }
    }
    return s;
}

In my userscript i'm including

  1. jQuery UI 1.11.4
  2. jQuery 1.11.1
  3. Bootstrap 3.3.5

I really don't get what's the problem since on debug console it is working without errors or something.

Devian
  • 817
  • 1
  • 12
  • 22
  • possible duplicate of [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 Aug 23 '15 at 17:37

1 Answers1

12

I assume that this is nothing but jQuery conflict with native prototype.

So only workaround I can find it to define properties using,

Object.defineProperty(Object.prototype, 'Count ',{
  value : function() {},
  enumerable : false
});

Object.defineProperty(Object.prototype, 'GetEntry  ',{
  value : function() {},
  enumerable : false
});

jQuery conflict with native prototype.

Community
  • 1
  • 1
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
  • Well... i thought jQuery.noConflict()... should work on this, i tried it but it didn't worked. It might be the fact that Site using its own jQuery so... things are getting messy. Thank you for this reply. It solved the problem, and beside that, i learnt a new way of declaring prototypes. – Devian Aug 23 '15 at 17:37
  • Please note that the above solution will not work on IE8 (at least it didn't in my tests). – Eugen Mihailescu Feb 15 '16 at 20:29