Skip to content

Instantly share code, notes, and snippets.

@clamstew
Created May 30, 2014 16:10
Show Gist options
  • Save clamstew/f6903d16a88b59464295 to your computer and use it in GitHub Desktop.
Save clamstew/f6903d16a88b59464295 to your computer and use it in GitHub Desktop.
Example of using call in a foreach function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Looking at Call</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- body goes here -->
<script type="text/javascript">
var forEach = function(list, callback) {
for (var n = 0; n < list.length; n++ ) {
callback.call(list[n], n);
}
};
var tools = ['ruby', 'javascript', 'stylesheets'];
forEach(tools, function(index) {
console.log( this == tools[index], "Got the expected value of ", tools[index] );
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment