Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserScript issue with Object.prototype

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.

like image 582
Devian Avatar asked Aug 23 '15 17:08

Devian


1 Answers

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.

like image 157
Deepak Ingole Avatar answered Nov 09 '22 22:11

Deepak Ingole