Tag Archive for function

Javascript Closure

/*
* Since we are passing the inner function outside, a reference to it
* still exists, hence it is not collected by the garbage collector.
*
* The variable a persists through to the next increment, and is not overwritten
* by outer() var a = 0 since inner still has a local variable inside it's scope.
*
* By creating global2, you have created another scope that acts independently,
* even though they are using the same function literal!
*/
function outer(){//define outer scope
var a = 0;//this is the persistent variable
function inner(){//define the inner scope
a++;//increment a
console.log(a);
}
return inner;//IMPORTANT: return the inner function to the outside (scope)
}
var global = outer();//create a new scope in global
global();//a = 1
global();//a = 2
var global2 = outer();
global2();//a = 1
global2();//a = 2

source

Function call() Method

function containerScope(){
var comp = { //create a new object
a: 22, //set some properties
b: 33
};
function square(x){ //create a new function
this.result = x*x;//this refers to the object when used in call()
}
square.call(comp, 4); //call the square function as a method of comp object
console.dir(comp); //note how a comp.result property has now been created
}

source

Array Functions as Data

function functionsDontNeedNames(){
var a = new Array(3);//create array length 3
a[0] = function(x){return x*x};//this is now the function
a[1] = 4;//will be used as 'x'
a[2] = a[0](a[1]);//a[2] now equals 16!
}

source

Accessing Arguments Using the Arguments Object

function containerScope(){
var a = [];
var b = {};
var c = true;
var d = 22;
function argumentsObject(a, b, c, d){
console.log(arguments.length);
console.log(arguments[0]);//Array
console.log(arguments[1]);//Object
console.log(arguments[2]);//true
console.log(arguments[3]);//22
arguments[3] = 55;//I have now changed d as well!
console.log(arguments[3]);
console.log(d);
}
argumentsObject(a, b, c, d);
}

source

Optional Function Arguments

function optionalArguments(a, /*optional*/b){
if(!b){b = []};//If b isn't set, create an empty array
//OR shorthand for this
b = b || [];
/*
* The OR operator looks to see if b is true, if it is it skips
* creating the Array. If b is false it creates the array. Not
* very readable.
*/
}

source

Function Template

/*
Function:	<Function, ex. my_function( $param1, $param2 )>
Description:	<Description of what it does>
Parameters:	<Parameters for function>
Example:	<Show an example of your function in use, ex. my_function( "thisparameter", "newparameter");>
Requirements:	<Anything that your function requires>
Notes:		<Any other comments you have>
Author:		<your name and email/website address>
*/

source

Prevent SQL Injection

/*
Function:	sql_sanitize( $sCode )
Description:	"Sanitize" a string of SQL code to prevent SQL injection.
Parameters:	$sCode
The SQL code which you wish to sanitize.
Example:	mysql_query('UPDATE table SET value="' . sql_sanitize("' SET id='4'") . '" WHERE id="1"');
Requirements:	PHP version 4 or greater
Notes:
Author:		engel <engel@engel.uk.to>
*/
function sql_sanitize( $sCode ) {
if ( function_exists( "mysql_real_escape_string" ) ) {		// If PHP version > 4.3.0
$sCode = mysql_real_escape_string( $sCode );		// Escape the MySQL string.
} else { // If PHP version < 4.3.0
$sCode = addslashes( $sCode );				// Precede sensitive characters with a backslash
}
return $sCode;							// Return the sanitized code
}

source

Random Number

function runMe(){
var test1 = function(){
alert("Test 1 has been fired");
}
var test2 = function(){
alert("Test 2 has been fired");
}
var myArray = [test1, test2]
var randomNumber = Math.floor(Math.random()*2);
myArray[randomNumber]();
}

source

Strip(i.e. remove) special character(s)

/* Strip(remove) special character(s) from a string.
Parameters -1-: String to process
-2-: Special character to strip
Note: If second parameter is null then the following list is used:
<tab>, <single quote>, <double quote>, <>, <|>, <`>, <~>, <^>
*/
FUNCTION strip_spc_character ( in_string IN varchar2,
in_chars  IN varchar2 DEFAULT null )
return varchar2
is
tab	              char(      1 ) := chr(9);
double_quote        char(      1 ) := chr(34);
single_quote        char(      1 ) := chr(39);
mask                varchar2( 80 ) := '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
v_special_chars     varchar2( 80 ) := '`~^|'||double_quote||single_quote||tab;
begin
if in_chars is not null then
v_special_chars := in_chars;
end if;
return translate( in_string, mask || v_special_chars, mask );
exception
when others then
return in_string;
end strip_spc_character;

source

TextMate snippet

trace('${1:0}.${2:_x}: '+${1:"$0"}.${2:_x});

source