Skip to content

Instantly share code, notes, and snippets.

View sarfraznawaz2005's full-sized avatar
🏠
Working from home

Sarfraz Ahmed sarfraznawaz2005

🏠
Working from home
View GitHub Profile
@sarfraznawaz2005
sarfraznawaz2005 / Get object methods.js
Last active October 12, 2015 18:30
Get object methods
// result will vary browser wise
alert(Object.getOwnPropertyNames(Function.prototype));
@sarfraznawaz2005
sarfraznawaz2005 / Extend built-in objects.js
Last active October 12, 2015 18:31
Extend built-in objects
//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"]
@sarfraznawaz2005
sarfraznawaz2005 / Better typeof.js
Last active October 12, 2015 18:31
Better typeof
// 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)
@sarfraznawaz2005
sarfraznawaz2005 / Concatenate.js
Last active October 12, 2015 18:31
Concatenate
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'
@sarfraznawaz2005
sarfraznawaz2005 / Template to add to jQuery.js
Last active October 12, 2015 18:31
Template to add to jQuery
(function($){
// store original reference to the method
var _old = $.fn.method;
$.fn.method = function(arg1,arg2){
if ( ... condition ... ) {
return ....
} else { // do the default
@sarfraznawaz2005
sarfraznawaz2005 / Quick way to round a number.js
Last active October 12, 2015 18:31
Quick way to round a number
console.log( 104.249834 | 0 ); //104
console.log( 9.999999 | 0 ); // 9
// bitwise operators operators call ToInt32 on each expression to be evaluated
@sarfraznawaz2005
sarfraznawaz2005 / Using length to truncate arrary.js
Last active October 12, 2015 18:31
Using length to truncate arrary
var foo = [1, 2, 3, 4, 5, 6];
foo.length = 3;
foo; // [1, 2, 3]
@sarfraznawaz2005
sarfraznawaz2005 / flatten multi-dimensional array.php
Last active October 12, 2015 18:31
flatten multi-dimensional array
<?php
/**
*
* @flatten multi-dimensional array
*
* @param array $array
*
* @return array
*
*/
@sarfraznawaz2005
sarfraznawaz2005 / Nice extend function.js
Last active October 12, 2015 18:31
Nice extend function
Function.prototype.extends = function(parent) {
var sConstructor = parent.toString();
var aMatch = sConstructor.match( /\s*function (.*)\(/ );
if ( aMatch != null ) { this.prototype[aMatch[1]] = parent; }
for (var m in parent.prototype) {
this.prototype[m] = parent.prototype[m];
}
};
// Note not Dog = Dog.extends(Animal)
Dog.extends(Animal);
@sarfraznawaz2005
sarfraznawaz2005 / PHP Database Backup Script.php
Last active October 12, 2015 18:32
PHP Database Backup Script
<?php
backup_tables('localhost','root','','lores_new');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);