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
// simple way to convert an object to an array with all butter of array using Array.slice | |
function foo(a, b, c){ | |
var argsArray = [].slice.apply(arguments); | |
argsArray.push(4,5); | |
alert(argsArray[4]); //5 | |
} | |
foo(1, 2, 3); |
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 format(string) { | |
var args = arguments; | |
var pattern = RegExp("%([1-" + (arguments.length-1) + "])", "g"); | |
return string.replace(pattern, function(match, index) { | |
return args[index]; | |
}); | |
}; | |
format("a %1 and a %2", "cat", "dog"); | |
//"a cat and a dog" |
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
// result will vary browser wise | |
alert(Object.getOwnPropertyNames(Function.prototype)); |
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
//create a new array method that removes a member | |
Array.prototype.remove = function(member) { | |
var index = this.indexOf(member); | |
if (index > -1) { | |
this.splice(index, 1); | |
} | |
return this; | |
} | |
['poppy', 'sesame', 'plain'].remove('poppy'); //["sesame", "plain"] |
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
// We know typeof has problms, here is a better solution | |
Object.toType = function(obj) { | |
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
} | |
// Examples: | |
Object.toType(window); //"global" (all browsers) | |
Object.toType([1,2,3]); //"array" (all browsers) |
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 concatenate(){ | |
// return arguments.join(''); // won't work. arguments is not a real array. | |
// return [].splice.call(arguments,0).join(''); // old 'n busted | |
return Array.prototype.join.call(arguments,''); // new hotness | |
} | |
concatenate('good',2,'go'); | |
// ==> 'good2go' |
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($){ | |
// store original reference to the method | |
var _old = $.fn.method; | |
$.fn.method = function(arg1,arg2){ | |
if ( ... condition ... ) { | |
return .... | |
} else { // do the default |
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
console.log( 104.249834 | 0 ); //104 | |
console.log( 9.999999 | 0 ); // 9 | |
// bitwise operators operators call ToInt32 on each expression to be evaluated |
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 = [1, 2, 3, 4, 5, 6]; | |
foo.length = 3; | |
foo; // [1, 2, 3] |
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
<?php | |
/** | |
* | |
* @flatten multi-dimensional array | |
* | |
* @param array $array | |
* | |
* @return array | |
* | |
*/ |