This file contains 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
// Define a function within itself | |
var myfunction = function fun(number) { | |
myfunction = function( number ) { | |
return ++number; | |
}; | |
return number * 2; | |
}; | |
alert(myfunction(10)); // 20 | |
alert(myfunction(10)); // 11 |
This file contains 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
function indexOfSmallest(a) { | |
var lowest = 0; | |
for (var i = 1; i < a.length; i++) { | |
if (a[i] < a[lowest]) lowest = i; | |
} | |
return lowest; | |
} | |
var a = [1, 2, 3, 4, 4, 6, 5, 0, 9]; |
This file contains 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
// user input | |
var input = '3 5 10\n2 7 15\n5 10 50'; | |
// split on newline | |
var inputSplitOnNewLine = input.split('\n'); | |
// get and set 'A', 'B', and 'N' values | |
// call fizzBuzz function | |
for (var i = 0; i < inputSplitOnNewLine.length; i++) { | |
var inputString = inputSplitOnNewLine[i]; // current 3 numbers |
This file contains 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
// Immediately invoked function | |
// Code treated as local variables and functions | |
// Anonymous function, wrapped by a set of parenthesis, allows execution of anonymous function | |
(function () { | |
var foo = 'hello, window'; | |
var bar = function () { | |
var foo = 'hello, function'; |
This file contains 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
// http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/ | |
/* Namespacing - The Module Pattern */ | |
var myApp = (function () { | |
var id = 0; | |
return { | |
next: function () { | |
return id++; | |
}, |
This file contains 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
/* functions */ | |
/* passing a function as a parameter */ | |
var calculate = function (number, paramTwo, fn) { | |
number = number + 3; | |
number = number + 1; | |
number = number * 8; | |
return fn(number, paramTwo); | |
}; |
This file contains 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
(function () { | |
var speed = 500, | |
i = 0, | |
doSomething = function () { | |
console.log('doSomething() executed ' + (i + 1) + ' times'); | |
i++; | |
if (i > 4) { | |
// stop when condition is met |
This file contains 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
(function () { | |
// HTML items | |
// <button>Normal</button> | |
// <button>Changed</button> | |
// <a href="http://www.google.com">Google</a> | |
// <a href="http://www.google.com">Google</a> | |
var buttons = document.getElementByTagName('button'); |
This file contains 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
// eventUtility.js | |
var eventUtility = { | |
addEvent: function (el, type, fn) { | |
/* | |
el: HTML element object (DOM object) | |
type: event type to listen to | |
fn: function | |
*/ |
This file contains 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
// event listener on each child node | |
$('#parent li').on('click', function(e) { | |
console.log('List item:', e.target.id, 'was clicked'); | |
}); |
OlderNewer