Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this JavaScript prototype function break jQuery?
    primarykey
    data
    text
    <p>Question:</p> <p>As soon as I add the below code to my html page, I get:</p> <pre><code>Line: 4 Error: Object doesn't support the property or method "exec". </code></pre> <p>This is the prototype that causes the bug:</p> <pre><code> Object.prototype.allKeys = function () { var keys = []; for (var key in this) { // Very important to check for dictionary.hasOwnProperty(key) // otherwise you may end up with methods from the prototype chain.. if (this.hasOwnProperty(key)) { keys.push(key); //alert(key); } // End if (dict.hasOwnProperty(key)) } // Next key keys.sort(); return keys; }; // End Extension Function allKeys </code></pre> <p>And this is the minimum code required to reproduce the error (Browser in question: IE9):</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;TestPage&lt;/title&gt; &lt;script type="text/javascript" src="jquery-1.9.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; /* Object.prototype.getName111 = function () { var funcNameRegex = /function (.{1,})\(/; var results = (funcNameRegex).exec((this).constructor.toString()); return (results &amp;&amp; results.length &gt; 1) ? results[1] : ""; }; // End Function getName */ Object.prototype.allKeys = function () { var keys = []; for (var key in this) { // Very important to check for dictionary.hasOwnProperty(key) // otherwise you may end up with methods from the prototype chain.. if (this.hasOwnProperty(key)) { keys.push(key); //alert(key); } // End if (dict.hasOwnProperty(key)) } // Next key keys.sort(); return keys; }; // End Extension Function allKeys &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;select id="selLayers" name="myddl"&gt; &lt;option value="1"&gt;One&lt;/option&gt; &lt;option value="2"&gt;Twooo&lt;/option&gt; &lt;option value="3"&gt;Three&lt;/option&gt; &lt;option value="4"&gt;Text1&lt;/option&gt; &lt;option value="5"&gt;Text2&lt;/option&gt; &lt;/select&gt; &lt;script type="text/javascript"&gt; //var dict = { "de": { "Text1": "Ersetzung 1", "Text2": "Ersetzung 2" }, "fr": { "Text1": "Replacement 1", "Text2": "Réplacement 2" }, "it": { "Text1": "Replacemente 1", "Text2": "Replacemente 2" }, "en": { "Text1": "Replacement 1", "Text2": "Replacement 2"} }; /* var languages = dict.allKeys(); for (var j = 0; j &lt; languages.length; ++j) { var strCurrentLanguage = languages[j]; var dictReplacements = dict[strCurrentLanguage] var keys = dictReplacements.allKeys(); //alert(JSON.stringify(dictReplacements)); //alert(JSON.stringify(keys)); for (var i = 0; i &lt; keys.length; ++i) { var strKey = keys[i]; var strReplacement = dictReplacements[strKey]; alert(strKey + " ==&gt; " + strReplacement); //alert('#selLayers option:contains("' + strKey + '")'); //$('#selLayers option:contains("' + strKey + '")').html(strReplacement); //$('#selLayers option:contains("Text1")').html("foobar"); } } */ $('#selLayers option:contains("Twooo")').text('Fish'); //alert(dict.allKeys()); //alert(dict["de"]["abc"]); /* $('#selLayers option[value=2]').text('Fish'); $('#selLayers option:contains("Twooo")').text('Fish'); $('#selLayers option:contains("Twooo")').html('&amp;Eacute;tage'); // http://stackoverflow.com/questions/7344220/jquery-selector-contains-to-equals $("#list option[value=2]").text(); $("#list option:selected").each(function () { alert($(this).text()); }); $("#list").change(function() { alert($(this).find("option:selected").text()+' clicked!'); }); */ &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I tried renaming the prototype function, just in case it conflicts with any jquery prototype, but that doesn't help at all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload