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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Please note that the above solution will not work on IE8 (at least it didn't in my tests).

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.