Created
May 30, 2014 16:10
-
-
Save clamstew/f6903d16a88b59464295 to your computer and use it in GitHub Desktop.
Example of using call in a foreach function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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