[JavaScript] Defining and invoking functions, Function Arguments

Chapter 8

When a function is invoked on an object, the function is called a method, and the object on which it is invoked is passed as an implicit argument of the function.

 

JavaScript supports quite a few built-in functions, such as eval(), parseInt().

But, which object do the function belong to??

 

 

Section 8.1

Functions may be defined to expect varying numbers of arguments

 

If the return statement does not have an associated expression, it returns the undefined value

If a function does not contain a return statement, it returns the undefined value

 

Note that parameters of a function are normally available only within the body of the executing function: they cannot be accessed outside the body of the function or after the function has returned. (But see Section 8.8 for an important exception.)

 

JavaScript does not check whether you have passed the type of data that the function expects, JavaScript does not check whether you have passed the correct number of arguments either.

 

A function accesses the arguments by their position in the argument list rather than by name.

 

Nested functions may be defined only at the top level of the function within which they are nested. That is, they may not be defined within statement blocks, such as the body of an if statement of while loop.

 

function f(x) { return x*x; }              // function statement

var f = function(x) { return x*x; };       // function literal

 

 

A function literal is an expression that defines an unnamed function.

 

The syntax for function literal is an expression, while statement for function

 

Although function literals create unnamed functions, the syntax allows a function name to be optionally specified, which is useful when writing recursive functions that call themselves, e.g.  :

 

var f = function fact(x) { if (x <= 1) return 1; else return x*fact(x-1); };

1.    Define an unnamed function

2.    The reference to this function is stored in the variable f rather than fact

3.    The function body is allowed to refer to fact

 

Function literals are created by JavaScript expression rather than statements

 

The function specified by a function literal expression can be stored into a variable, passed to another function, or even invoked directly:

f[0] = function(x) { return x*x; };  // Define a function and store it
a.sort(function(a,b){return a-b;});  // Define a function; pass it to another
var tensquared = (function(x) {return x*x;})(10);  // Define and invoke

 

Recall from Chapter 2 that dollar signs and underscores ($ _ ) are the two characters besides letters and numbers that are legal in JavaScript identifiers.

 

 

Chapter 8.2

 

JavaScript functions can be invoked with any number of arguments, regardless of the number of arguments named in the function definition.

It’s legal to pass values of any type to any function, because a function is loosely typed, there is no way for it to declare the type of arguments it expects.

 

 

a = a || [];

Recall from Chapter5 that the || operator returns its first argument if the argument is true or value that converts to true, otherwise, it returns its second argument.

 

Arguments object is an array-like object that allows the argument values passed to the function to be retrieved by number, rather than by name, and it also defines an additional callee property.

 

 

Generally,

            function test()         

                  var abc  = "I'm abc";

                 var arrayTest = [abc,'xyz'];

                 abc = "abc has been changed";

 

                 alert(arrayTest[0]);   // I'm abc

                 alert(abc);          // abc has been changed

                 alert(arrayTest[1]);   // xyz

            }

But if arguments is referred, it is another case

            function f(x) {

             alert(x);             // Displays the initial value of the argument

                    arguments[0] = null;   // Changing the array element also changes x!

             alert(x);             // Now displays "null"

            }

 

Finally, bear in mind that arguments is just an ordinary JavaScript identifier, not a reserved word. If a function has an argument or local variable with that name, it hides the reference to the Arguments object. For this reason, it is good idea to treat arguments as a reserved word and avoiding using it as a variable name.

 

 

arguments has a property called callee, which refers to the function itself.

 

 

Since JavaScript is loosely typed, method arguments have no declared types, and no type checking is performed on the values passed to a function.

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值