Defining functions
There are two ways to give names to functions
Function declaration
function myFunc1()
{
}
>>> function sum(a, b) {return a + b;};
>>> sum(1, 2)
3
Function expression
var myFunc2 = function()
{
}
>>> var sum = function(a, b) {return a + b;};
>>> sum(1, 2)
3
Using the function constructor
There is a built-in constructor function called Function()
>>> var sum = new Function('a', 'b', 'return a + b;');
>>> sum(1, 2)
3
Difference between function declaration and expression
a function declared as a named function expression cannot be used in code until after it is encountered and defined. On the other hand, the function defined using the function declaration method can be called whenever you want to on the page, even as an onload handler.
Drawbacks of using a Function constructor
When using the Function() constructor, you pass the parameter names first (as strings) and then the source code for the body of the function (again as a string).
This source code evaluation suffers from the same drawbacks as the eval() function, so defining functions using the Function() constructor should be avoided when possible
stay away from cases where you pass JavaScript code as a string
Return values
The return type can be a number, a string of text, or even a DOM element (object).
The default return value is undefined
Properties of the Function Objects
>>> function myfunc(a){return a;}
>>> myfunc.constructor
Function()
Functions also have a length property, which contains the number of parameters the function accepts
>>> function myfunc(a, b, c){return true;}
>>> myfunc.length
3
which doesn't exist in the ECMA standard, but is implemented across the browsers—the caller property. This returns a reference to the function that called our function.
>>> function A(){return A.caller;}
>>> function B(){return A();}
>>> B()
B()
If you call A() from the global space (outside of any function), A.caller will be null
The most important property of a function is the prototype property
Methods of the Function Objects
toString()
>>> function myfunc(a, b, c) {return a + b + c;}
>>> myfunc.toString()
"function myfunc(a, b, c) {
return a + b + c;
}"
var some_obj = {
name : 'Ninja',
say : function(who) {
return 'Haya ' + who + ', I am a ' + this.name;
}
}
>>> some_obj.say('Dude');
"Haya Dude, I am a Ninja"
call()
the first parameters to call is an object that this refers to within the function definition
so my_obj is referred to by this in function definition that say points to.
>>> my_obj = {name: 'Scripting guru'};
>>> some_obj.say.call(my_obj, 'Dude');
"Haya Dude, I am a Scripting guru"
apply()
The method apply() works the same way as call() but with the difference that all parameters you want to pass to the method of the other object are passed as an array
some_obj.someMethod.apply(my_obj, ['a', 'b', 'c']);
>>> some_obj.say.apply(my_obj, ['Dude']);
"Haya Dude, I am a Scripting guru"
The arguments Object Revisited
arguments looks like an array but is actually an array-like object
It just has the length property but it doesn't provide any of the array methods, such as sort() or slice()
The arguments object has another interesting property—the callee property. This contains a reference to the function being called
>>> function f(){return arguments.callee;}
>>> f()
f()
arguments.callee allows anonymous functions to call themselves recursively
(function(count) {
if (count < 5) {
alert(count);
arguments.callee(++count);
}
})(1)
No comments:
Post a Comment