Why does this JavaScript prototype function break jQuery? -
question:
as add together below code html page, get:
line: 4 error: object doesn't back upwards property or method "exec".
this prototype causes bug:
object.prototype.allkeys = function () { var keys = []; (var key in this) { // of import check dictionary.hasownproperty(key) // otherwise may end methods prototype chain.. if (this.hasownproperty(key)) { keys.push(key); //alert(key); } // end if (dict.hasownproperty(key)) } // next key keys.sort(); homecoming keys; }; // end extension function allkeys
and minimum code required reproduce error (browser in question: ie9):
<!doctype html> <html> <head> <title>testpage</title> <script type="text/javascript" src="jquery-1.9.1.min.js"></script> <script type="text/javascript"> /* object.prototype.getname111 = function () { var funcnameregex = /function (.{1,})\(/; var results = (funcnameregex).exec((this).constructor.tostring()); homecoming (results && results.length > 1) ? results[1] : ""; }; // end function getname */ object.prototype.allkeys = function () { var keys = []; (var key in this) { // of import check dictionary.hasownproperty(key) // otherwise may end methods prototype chain.. if (this.hasownproperty(key)) { keys.push(key); //alert(key); } // end if (dict.hasownproperty(key)) } // next key keys.sort(); homecoming keys; }; // end extension function allkeys </script> </head> <body> <select id="sellayers" name="myddl"> <option value="1">one</option> <option value="2">twooo</option> <option value="3">three</option> <option value="4">text1</option> <option value="5">text2</option> </select> <script type="text/javascript"> //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(); (var j = 0; j < languages.length; ++j) { var strcurrentlanguage = languages[j]; var dictreplacements = dict[strcurrentlanguage] var keys = dictreplacements.allkeys(); //alert(json.stringify(dictreplacements)); //alert(json.stringify(keys)); (var = 0; < keys.length; ++i) { var strkey = keys[i]; var strreplacement = dictreplacements[strkey]; alert(strkey + " ==> " + 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('é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!'); }); */ </script> </body> </html>
i tried renaming prototype function, in case conflicts jquery prototype, doesn't help @ all.
because going add together enumerable item every single object. sizzle (which jquery uses) uses object literals configure selector parsing. when loops these config objects tokens, doesn't expect function. in case, it's trying utilize function regexp
.
imagine scenario:
var obj = { a: 1, b: 2, c: 3 }; var result = 0; (var prop in obj) { // on 1 of these iterations, `prop` "allkeys". // in case, `obj[prop]` function instead of number. result += obj[prop] * 2; } console.log(result);
if have added object
's prototype can't used number, nan
result.
a solution problem add together allkeys
function object
instead of object.prototype
. mimics object.keys
:
object.allkeys = function (obj) { var keys = []; (var key in obj) { // of import check dictionary.hasownproperty(key) // otherwise may end methods prototype chain.. if (obj.hasownproperty(key)) { keys.push(key); //alert(key); } // end if (dict.hasownproperty(key)) } // next key keys.sort(); homecoming keys; }; // end extension function allkeys
javascript jquery xhtml
No comments:
Post a Comment