Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created June 4, 2012 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/2871510 to your computer and use it in GitHub Desktop.
Save rwaldron/2871510 to your computer and use it in GitHub Desktop.
jQuery.each() w/ fat arrow
/**
*
* paste this code into:
*
* http://traceur-compiler.googlecode.com/git/demo/repl.html
*
*
*/
if (!window.jQuery) {
document.head.appendChild(
document.createElement("script").{
src = "http://code.jquery.com/jquery.js";
}
);
}
jQuery.each([ "foo", "bar", "baz" ], (i, item) => {
console.log(item);
});
@FrankFang
Copy link

I pasted the code into repl.html, but it says
repl(10, 1): jQuery is not defined

@rwaldron
Copy link
Author

rwaldron commented Jun 5, 2012

Yeah, looks like jQuery hasn't loaded yet...

I actually typed this in by hand, so by the time I got to jQuery.each the top was parsed and executed.

@rwaldron
Copy link
Author

rwaldron commented Jun 5, 2012

This seems to work for a "drop in" paste...

if ( !("jQuery" in window) ) {
  document.head.appendChild(
    document.createElement("script").{
      src = "http://code.jquery.com/jquery.js";
      onload = function() {
        window.jQuery.each([ "foo", "bar", "baz" ], (i, item) => {

          console.log( item );
        });
      };
    }
  );
}

@FrankFang
Copy link

thx, it works.
but I never saw the keyword 'let' in JavaScript. I am confused.

@rwaldron
Copy link
Author

rwaldron commented Jun 5, 2012

let is like var but its block scoped instead of lexically scoped. It's new in E6

@rwaldron
Copy link
Author

rwaldron commented Jun 5, 2012

TBH, this isnt a very interesting use of let - I'm also jut getting accustomed to these new language features

@rwaldron
Copy link
Author

rwaldron commented Jun 5, 2012

Simple let:

if (true) {
  let a = "alpha";
}

console.log( "a" in this ); //  false

I updated the snippet above... the script variable was actually unnecessary

@FrankFang
Copy link

got it. thx. I am ready to learn sth. about E6 too.

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