Skip to content

Instantly share code, notes, and snippets.

@TheSecretSquad
Last active July 8, 2017 21:04
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 TheSecretSquad/2b212dd804c580bfff05 to your computer and use it in GitHub Desktop.
Save TheSecretSquad/2b212dd804c580bfff05 to your computer and use it in GitHub Desktop.
Example code from http://benalman.com/news/2010/11/immediately-invoked-function-expression/ written with JQuery and Amber Smalltalk
'a' asJQuery each: [ :index :value |
value asJQuery on: 'click' do: [ :event |
event preventDefault.
window alert: 'I am link #' , index
]
]
$('a').each(function(index, value) {
$(value).on("click", function(event) {
event.preventDefault();
window.alert("I am link #" + index);
});
});
@TheSecretSquad
Copy link
Author

Original JavaScript verison:

var elems = document.getElementsByTagName( 'a' );
for ( var i = 0; i < elems.length; i++ ) {
    elems[ i ].addEventListener( 'click', (function( lockedInIndex ){
        return function(e){
            e.preventDefault();
            alert( 'I am link #' + lockedInIndex );
        };
    })( i ), 'false' );
}

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