Friday 15 February 2013

javascript - Array prototype available in forEach -



javascript - Array prototype available in forEach -

i have polyfill array.find() got mdn

the browser i'm testing in doesn't have back upwards polyfill runs , functions intended. plugin have foreach on array pass , lastly element in function find.

why happening?

also when inspect array in chrome's devtools following.

class="snippet-code-js lang-js prettyprint-override">if (!array.find) { $('<h5>this browser doesnt back upwards array.find</h5>').appendto('body'); } if (!array.prototype.find) { array.prototype.find = function(predicate) { if (this == null) { throw new typeerror('array.prototype.find called on null or undefined'); } if (typeof predicate !== 'function') { throw new typeerror('predicate must function'); } var list = object(this); var length = list.length >>> 0; var thisarg = arguments[1]; var value; (var = 0; < length; i++) { value = list[i]; if (predicate.call(thisarg, value, i, list)) { homecoming value; } } homecoming undefined; }; } var somearray = [{name: 'something'}, {name: 'something else'}]; somearray.foreach(function (item, index) { $('<h4>'+ item.name +'</h4>').appendto('body'); }); var extended = $.extend({}, [], somearray); $.each(extended, function (index, item) { $('<h4>'+ item.name +'</h4>').appendto('body'); }); if (extended.find) { $("<div>notice have in extended function find<div>").appendto('body'); } class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

notice find in __proto__ darker shade of pinkish other proto's.

any ideas?

your foreach extension checking keys not checking own-properties of object. is, code:

array.prototype.x = 3; var test = [1]; (var key in test) { console.log(key); }

...will log both 0 , x because x enumerable key in [] not own-property of []. prepare can either patch foreach code check own-properties only:

array.prototype.x = 3; var test = [1]; (var key in test) { if (test.hasownproperty(key)) { console.log(key); } }

or can alter way set key on prototype, other keys on prototype, not enumerable. revised poyfill be:

(function () { // scope bracket; hides `find` global scope. function find(predicate) { if (this == null) { throw new typeerror('array.prototype.find called on null or undefined'); } if (typeof predicate !== 'function') { throw new typeerror('predicate must function'); } var list = object(this), length = list.length >>> 0, thisarg = arguments[1], value, i; (i = 0; < length; i++) { value = list[i]; if (predicate.call(thisarg, value, i, list)) { homecoming value; } } homecoming undefined; }; if (!array.prototype.find) { if (object.defineproperty) { object.defineproperty(array.prototype, 'find', {value: find, enumerable: false}); } else { array.prototype.find = find; } } } ());

if browser not back upwards array.prototype.find, however, there chance not back upwards object.defineproperty, leaving in same place before. need hope not old lacks .hasownproperty() testing, , you'll need modify foreach function. (the array.prototype.find() function part of ecmascript 6 draft; object.defineproperty() standardized in ecmascript 5.1, while object.prototype.hasownproperty() ecmascript 3. es5.1 released in 2011 long browser couple years old, above should work -- in particular if you're using current opera or ie, don't have new es6 .find() in them yet, have object.defineproperty.

javascript arrays prototype

No comments:

Post a Comment