Created
September 14, 2012 18:31
-
-
Save bmakarand2009/3723789 to your computer and use it in GitHub Desktop.
Javascript Functions
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
//by default, a function has access to this and arguments objects. | |
//arguments.callee() function calls the function itself, to use for recursive | |
function doSomething(){ | |
arguments.callee(); //gives the name of function, without having to explicity name it | |
} |
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 utility function which will generate unique IDs | |
http://jsfiddle.net/mbhatamb/pzXtd/ | |
challenges : we cannot make it a global function since it can conflict with 3rd party, also we dont want to eat up memory | |
by calling the function again and again. | |
solution : we will create utility with a nameGen closure, and invoke it to create unique IDS | |
*/ | |
var utility = (function() { | |
var i = 0; | |
return{ | |
//this is as good as saying namegen is a method inside | |
namegen: function() { | |
var custName = "myName" + i; | |
i++; | |
return custName; | |
} | |
} | |
}()); | |
var n1 = utility.namegen(); | |
var n2 = utility.namegen(); | |
console.log("name1 is" + n1 + " name2 is" + n2); |
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://jsfiddle.net/mbhatamb/cX5CP/ | |
//simple declaration for a closure | |
//closures are functions around functions or arguments.. | |
function doSomething(myarg) { | |
//this creates a clsoure for myarg | |
return function() { | |
alert(myarg); | |
}; | |
} | |
var myfn = doSomething("hello123"); | |
console.log(myfn); | |
myfn(); //execute the returned function, which is the closure |
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
//difference between declaring a function vs function expressions. | |
//also see calculate method | |
doSomething("sample arg"); //this function would get called eventhough it is declared later | |
doSomethingExp();//this will given an error here, that isthe main difference | |
function doSomething() { | |
console.log("argument passed is" + arguments[0]); | |
alert ("this is the doSomething() function"); | |
} | |
var doSomethingExp = function() { | |
alert("this function is declared as a expression"); | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
/** here we define a immediately invokved function(), and this is how coding | |
is done, so that Global Variables conflict can be avoided, when using multiple scripts ( function(){ }());*/ | |
(function(){ | |
var foo=5; | |
alert("hello world"); | |
myfunc(); | |
function myfunc(){ | |
console.log(foo); | |
} | |
onemorefunc(); | |
}()); | |
function onemorefunc(){ | |
alert("one more func called"); | |
console.log(window.foo); | |
} | |
</script> | |
</head> | |
<body> | |
<p id="intro">Hello World!</p> | |
<div class="mytop"> | |
<p> This is the first para </p> | |
<p> This is the second para </p> | |
<div> | |
This is one more div </div> | |
</div> | |
</body> | |
</html> | |
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
calculate = function(x, y, func) { | |
return func(x, y) | |
} | |
var add = function(x, y) { | |
return x + y; | |
} | |
var multiply = function(x, y) { | |
return x * y; | |
} | |
var result = calculate(5, 6, add); | |
console.log(result); |
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
//Just a Calculator Example, Version2 | |
// arguments is not a Array but like an array object, so we will use call | |
//Refer also to Javascript-class (call, apply method example) | |
//we dont want to restric to two elements, hence will use the arguments object | |
//http://jsfiddle.net/mbhatamb/ptjea/ | |
calculate = function() { | |
//final argument is the function object | |
//as pop method returns the final object and remove it from the array | |
var func = Array.prototype.pop.apply(arguments); | |
return func.apply(null,arguments); | |
} | |
var add = function(x, y) { | |
return x + y; | |
} | |
var multiply = function(x, y) { | |
return x * y; | |
} | |
var result = calculate(5, 6, add); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment