Created
October 22, 2008 18:32
-
-
Save kassens/18744 to your computer and use it in GitHub Desktop.
js quiz
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
/* | |
* Author: Jan Kassens | |
* | |
* Guess the results and run it in Firebug | |
* If you got all right with the first try you're a JS Samurai (Ninja is overused) | |
*/ | |
var tasks = { | |
'try/finally': function(){ | |
var value = 'foo'; | |
var fn = function(change){ | |
try { | |
return value; | |
} finally { | |
if (change){ | |
value = 'bar'; | |
return 'zomg'; | |
} | |
} | |
}; | |
console.log(fn()); | |
console.log(fn(true)); | |
console.log(fn()); | |
}, | |
numbers: function(){ | |
console.log(060 + 070 + 080 + 090); | |
var x = '4', y = '4'; | |
x += 0; | |
y *= 1; | |
console.log(x + y); | |
}, | |
scope: function(){ | |
var scope = 'outer'; | |
(function(){ | |
if (true) var scope = 'inner'; | |
console.log(scope); | |
})(); | |
scope = 'outer'; | |
(function(){ | |
if (false) var scope = 'inner'; | |
console.log(scope); | |
})(); | |
scope = 'outer'; | |
(function(){ | |
if (!scope) var scope = 'inner'; | |
console.log(scope); | |
})(); | |
}, | |
'truthy?': function(){ | |
(function(thing){ | |
console.log(">%s< is ", thing.toString(), thing ? 'truthy' : 'falsy'); | |
return arguments.callee; | |
}) | |
(0) | |
(1) | |
('0') | |
('1') | |
(Number(0)) | |
(new Number(0)) | |
(new Number(0) + new Number(0)) | |
('') | |
(String('')) | |
(new String('')) | |
(new String('') + new String('')) | |
([]) | |
({}); | |
}, | |
prototypes: function(){ | |
try { | |
var result = Array.prototype.join.call("input", " "); | |
console.log(result); | |
} catch (e){ | |
console.log('error'); | |
} | |
try { | |
var result = Array.prototype.reverse.call("input", " ").join(" "); | |
console.log(result); | |
} catch (e){ | |
console.log('error'); | |
} | |
}, | |
objects: function(){ | |
var f = function(){ | |
return | |
{ | |
x: 'one' | |
}; | |
}; | |
var g = function(){return {x: 'two'};}(); | |
var h = {x: 'three'}; | |
console.log((f() || g || h).x); | |
} | |
}; | |
for (task in tasks){ | |
console.group(task); | |
tasks[task](); | |
console.groupEnd(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment