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
// http://coderbyte.com/CodingArea/Results.php?ct=Letter%20Capitalize | |
function LetterCapitalize(str) { | |
return str.replace(/_/g, " ").toLowerCase().replace(/\w\S*/g, function (txt) { | |
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); | |
}); | |
} |
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
// all objects inherit methods and properties from Object.prototype, although they may be overridden | |
Object.prototype.toSource() | |
// "({})" | |
str = 'hello'; | |
arr = new Array(20); | |
num = 99; | |
Object.prototype.boom = 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
function identity(x) { | |
return x; | |
} | |
var heap = new BinaryHeap(identity); | |
forEach([2, 4, 5, 1, 6, 3], function(number) { | |
heap.push(number); | |
}); | |
while (heap.size() > 0) | |
show(heap.pop()); |
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
# http://bagwanpankaj.com/metaprogramming-in-rails | |
class Testt | |
end | |
Testt.class_eval do | |
def foo | |
"I'll be available as instance method" | |
end |
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
#first we create a subclass of class string | |
class MyString < String | |
end | |
MyString.new | |
# => "" | |
#now we are going to override this method by some Ruby magic | |
MyString.class_eval do | |
def empty? |
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
var foo = { | |
toString: function () { | |
return 5; | |
}, | |
valueOf: function () { | |
return "foo"; | |
} | |
}; | |
alert(foo.toString() + 1); // 6 (bad!) | |
alert(foo + 1); // "foo1" (no good!) |
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
[[]][0] === [] | |
++[[]][0] === 1 | |
++[[]][+[]] === 1 // yay! wtf! |
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
// Of course, the variable is undefined because its being declared but not initialized until after the document.write in the function context which itself runs before the first declaration of a. | |
var a = 8; | |
var someFunc = function(){ | |
document.write(a); | |
var a = 8; | |
}; | |
someFunc(); // writes undefined |
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
// Remember how true sometimes has a value so in the above 3 > 2 evaluates to true making the second part of the expression evaluate true > 1 which is false. | |
3 > 2 > 1 // false |