Skip to content

Instantly share code, notes, and snippets.

@jney
Forked from 140bytes/LICENSE.txt
Created June 21, 2011 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jney/1037625 to your computer and use it in GitHub Desktop.
Save jney/1037625 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

The 140byt.es site hasn't launched yet, but for now follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt. It was inspired by work from Thomas Fuchs and Dustin Diaz.

Array.prototype.forEach = [].forEach // if forEach already exist we just keep it
|| function ( // else declare function forEach that
fn, // takes a fn parameter as a function
context // and a context parameter
) { //
for (var i=0,that=this;i<that.length;i++) { // for each element of the current array
if (i in that) { // ...
fn.call(context, that[i], i, that); // we call the function in a defined context
}
}
};
Array.prototype.forEach=[].forEach||function(a,b){for(var c=0,d=this;c<d.length;c++)c in d&&a.call(b,d[c],c,d)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "forEach",
"description": "polyfill an ES5-compatibile Array.forEach where needed.",
"keywords": [
"polyfill",
"es5",
"forEach"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>false, true, false</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
Array.prototype.forEach=[].forEach||function(a,b){for(var c=0,d=this;c<d.length;c++)c in d&&a.call(b,d[c],c,d)}
var ret = [], test = {something: 2};
[1,2,3].forEach(function(e) { ret.push(e === this.something) }, test);
document.getElementById( "ret" ).innerHTML = ret;
</script>
@michaelficarra
Copy link

Only four characters more and it actually obeys the spec:

'forEach'in[]||(Array.prototype.forEach=function(a,b){for(var c=0,d=this.length;c<d;c++)a.call(b,this[c],c,this)})
Array.prototype.forEach=[].forEach||function(a,b){for(var c=0;c<this.length;c++)if(c in this)a.call(b,this[c],c,this)}

@jney
Copy link
Author

jney commented Jun 22, 2011

i don't understand what you mean, why should i use Array.prototype.forEach=[].forEach||... instead of 'forEach'in[]||... ?

@michaelficarra
Copy link

Fewer characters...

@jney
Copy link
Author

jney commented Jun 22, 2011

'forEach'in[]||... is shorter... no ?

"'forEach'in[]||(Array.prototype.forEach=function(a,b){for(var c=0,d=this.length;c<d;c++)a.call(b,this[c],c,this)})".length // 114

"Array.prototype.forEach=[].forEach||function(a,b){for(var c=0;c<this.length;c++)if(c in this)a.call(b,this[c],c,this)}".length // 118

@michaelficarra
Copy link

No.

'forEach'in[]||(Array.prototype.forEach=)
Array.prototype.forEach=[].forEach||

The 4 additional characters in mine are from the addition of the condition that c in this (to follow spec) minus the removal of the length cache. It will be slower and do an unnecessary assignment, but it will follow spec.

@jney
Copy link
Author

jney commented Jun 22, 2011

I read your code too quickly. I will update this gist. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment