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 an object using the Revealing Module Pattern | |
var myCounter = function() { | |
// Both Private and Public methods and properties are defined here | |
var counter = 0; | |
function count() { | |
return 0+counter; | |
} |
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
var foo = []; | |
setInterval(function() { | |
foo.push(Math.random()); | |
if(foo.length > 200) { | |
foo = foo.slice(-200); | |
} | |
console.log(foo.length); | |
}, 10); |
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
// the following code outputs: 999999999 | |
var someArray = [1,2,3,4,5,6,7,8,9]; | |
for(var i=0 ; i<someArray.length ; i++) { | |
performSomeAsyncOperation("foo", function hereIsACallback() { | |
console.log(someArray[i]); | |
}); | |
} |
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
var i=0; | |
// 01234 | |
for(i=0 ; i<5 ; i++) { | |
console.log('standard for loop', i); | |
} | |
// 55555 | |
for(i=0 ; i<5 ; i++) { | |
setTimeout(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
var handling = false; | |
window.onSomeEvent = function handleEvent() { | |
if(handling) return; | |
handling = true; | |
/* some other handler code here which eventually switches handling off again */ | |
}; |
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
var hasMultipleArgs = args.length > 1; | |
var argIsString = typeof arg == 'string'; | |
var argIsSentence = ~arg.indexOf(' '); | |
var argIsFormattingOption = ~arg.indexOf('%'); | |
if (hasMultipleArgs && argIsString && !argIsSentence && !argIsFormattingOption) { /*...*/ } | |
// see https://github.com/observing/devnull/blob/master/lib/logger.js#L364-367 for alternative style |
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
<html> | |
<head> | |
<script> | |
function foo() { | |
alert('bar'); | |
} | |
document.onclick = foo; | |
</script> |
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
Object.prototype.foo = 4; | |
var bar = {}; | |
bar.baz = 5; | |
for(var prop in bar) { | |
console.log(prop); | |
} | |
/* | |
logs: |
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 fitness(chromosome){ | |
// higher fitness is better | |
var f = 0; // start at 0 - the best fitness | |
for(var i=0, c=target.length ; i<c ; i++) { | |
// subtract the ascii difference between the target char and the chromosome char | |
// Thus 'c' is fitter than 'd' when compared to 'a'. | |
f -= Math.abs(target.charCodeAt(i)-chromosome[i]); | |
} | |
return f; | |
} |
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
<?php | |
class Foo { | |
public function __call($func, $args) { | |
if(method_exists($this,$func)) { | |
return call_user_func_array(array($this,$func),$args); | |
} | |
} | |
} |