JavaScript Function arguments
Views (1719)
In JavaScript, a function may be called with any number of arguments, no matter how many of them are listed.
Javascript doesn't support polymorphism.
Accessing the unnamed arguments
Arguments is not an array, it is special pseudo-array and almost same as Object. It only looks like array, because it’s keys are numeric and it has length.
function test(a,b) {
alert("a="+a+", b="+b)
}
test(1) // a=1, b=undefined
test(1,2) // a=1, b=2
test(1,2,3) // a=1, b=2, extra argument is not listed
Arguments which are not provided become undefined, for examplealert("a="+a+", b="+b)
}
test(1) // a=1, b=undefined
test(1,2) // a=1, b=2
test(1,2,3) // a=1, b=2, extra argument is not listed
test() function test(x) { alert(x === undefined) // true }
Javascript doesn't support polymorphism.
function poly(a) { alert("1"); }
function poly(a,b,c) { alert("2"); }
poly(1) // 2 = >first function is called poly(a,b,c)
poly(1,2,3) // 2 => second function is called poly(a,b,c)
function poly(a,b,c) { alert("2"); }
poly(1) // 2 = >first function is called poly(a,b,c)
poly(1,2,3) // 2 => second function is called poly(a,b,c)
Accessing the unnamed arguments
function testArg() {
//var args = [].slice.call(arguments)
for(var i=0; i<arguments.length; i++) {
alert("Hello, " + arguments[i]) }
testArg("Arg1", "Arg2") // 'Hello, Arg1', then 'Hello, Arg2'
Even if the function contains arguments, like: testArg(a,b,c) // 'Hello, Arg1', then 'Hello, Arg2'//var args = [].slice.call(arguments)
for(var i=0; i<arguments.length; i++) {
alert("Hello, " + arguments[i]) }
testArg("Arg1", "Arg2") // 'Hello, Arg1', then 'Hello, Arg2'
Arguments is not an array, it is special pseudo-array and almost same as Object. It only looks like array, because it’s keys are numeric and it has length.
function testArg() {
var a = arguments.shift() // error! arguments is not an array alert(a) }
var a = arguments.shift() // error! arguments is not an array alert(a) }
Using arguments as Array
function testArg() {
var args = [].join.call(arguments, ':') alert(args) // 1:2:3 } testArg(1,2,3)
var args = [].join.call(arguments, ':') alert(args) // 1:2:3 } testArg(1,2,3)
Check argument value
function checkArg(height, weight , error) {
if (height === undefined) height = 100
weight = weight || 200
error = error || "Warning";
alert(height); //100
alert(weight); //200
alert(error);
}
checkArg() // "100", "200", "Warning"
checkArg(null, null , "asda") // "", "200", "asda"
if (height === undefined) height = 100
weight = weight || 200
error = error || "Warning";
alert(height); //100
alert(weight); //200
alert(error);
}
checkArg() // "100", "200", "Warning"
checkArg(null, null , "asda") // "", "200", "asda"
arguments.callee - it references the function which is being run.
arguments.callee.caller - it keeps the reference to a calling function.
function main() { Hello(); }
function Hello() {
alert("caller is " + arguments.callee.caller.toString());
alert("caller is " + Hello.caller.toString());
alert("caller name is " + arguments.callee.caller.name);
alert("caller name is " + Hello.caller.name); }
main();
function Hello() {
alert("caller is " + arguments.callee.caller.toString());
alert("caller is " + Hello.caller.toString());
alert("caller name is " + arguments.callee.caller.name);
alert("caller name is " + Hello.caller.name); }
main();
call - func.call(obj, arg1, arg2,...)
var menu = {
title: "Top"
}
function getTitle() {
alert( this.title )
}
getTitle.call(menu) // "Top"
var menu = {
title: "Top",
subtitle: "SubTop"
}
function getTitle(a,b) {
alert( this[a] + " - " + this[b])
}
getTitle.call(menu, 'title', 'subtitle') // "Top"
title: "Top"
}
function getTitle() {
alert( this.title )
}
getTitle.call(menu) // "Top"
var menu = {
title: "Top",
subtitle: "SubTop"
}
function getTitle(a,b) {
alert( this[a] + " - " + this[b])
}
getTitle.call(menu, 'title', 'subtitle') // "Top"
apply - The func.apply is same as func.call, but it accepts an array of arguments instead of a list.
getTitle.call(menu, ['title', 'subtitle']) // "Top"
Function Expression
(function() {
var a, b // local variables
// ... // and the code
})()
Running in place is mostly used when we want to do the job involving local variables. We don’t want our local variables to become global, so wrap the code into a function.
After the execution, the global namespace is still clean.
function work() {}() // syntax error
function() {}() // error, function declaration without name.
var result = function(a,b) { return a+b }(2,2) => var result = sum(2,2)
alert(result) // 4
var a = 5
(function() {
alert(a)
})() // error , no semicolon after var a=5
var a = 5;
(function() {
alert(a)
})() //5
Static variables - variable is not cleared in next callsfunction() {}() // error, function declaration without name.
var result = function(a,b) { return a+b }(2,2) => var result = sum(2,2)
alert(result) // 4
var a = 5
(function() {
alert(a)
})() // error , no semicolon after var a=5
var a = 5;
(function() {
alert(a)
})() //5
// PHP code!
function f() {
static $count = 0;
echo ++$count;
}
f(); f(); f(); // 1 2 3
In JavaScriptstatic $count = 0;
echo ++$count;
}
f(); f(); f(); // 1 2 3
function f() {
f.count = ++f.count || 1 // f.count is undefined at first
alert("Call No " + f.count)
}
f(); f(); f();
f.count = ++f.count || 1 // f.count is undefined at first
alert("Call No " + f.count)
}
f(); f(); f();
A global variable can keep the counter, but static variables lead to a better architecture.
Static methods
function Menu(title) {
arguments.callee.count = ++arguments.callee.count || 1
this.title = title
}
Menu.getCount = function() {
alert( Menu.count )
}
var menu1 = new Menu("Menu1")
var menu2 = new Menu("Menu2")
Menu.getCount() // 2
arguments.callee.count = ++arguments.callee.count || 1
this.title = title
}
Menu.getCount = function() {
alert( Menu.count )
}
var menu1 = new Menu("Menu1")
var menu2 = new Menu("Menu2")
Menu.getCount() // 2
Typeof operator - takes a value and return it’s type
Operator: typeof x
Function-like: typeof(x)
typeof undefined // "undefined"
typeof 0 // "number"
typeof true // "boolean"
typeof "foo" // "string"
typeof {} // "object"
typeof null // "object"
typeof function(){} // "function"
typeof NaN // "number"
function f(x) {
if (typeof x == 'function') {
... // in case when x is a function
} else {
... // in other cases
}
} typeof 0 // "number"
typeof true // "boolean"
typeof "foo" // "string"
typeof {} // "object"
typeof null // "object"
typeof function(){} // "function"
typeof NaN // "number"
function f(x) {
if (typeof x == 'function') {
... // in case when x is a function
} else {
... // in other cases
}
This was originally posted here.

Like
Report
*This post is locked for comments