Javascript学习5 - 函数

简介: 原文:Javascript学习5 - 函数在Javascript中,函数和对象是交织在一起的。有些函数的特性与对象相关联。这一点的内容在第六部分会讨论到。 这一部分主要讨论函数与其它比较熟悉的语言(如C/C++)不同的地方 5.1 函数定义     function 函数名(参数1,参数2...)     {         主体;     }     函数内可以有return,也可以没有return. 没有return时,函数返回undefined值。
原文: Javascript学习5 - 函数

在Javascript中,函数和对象是交织在一起的。有些函数的特性与对象相关联。这一点的内容在第六部分会讨论到。
这一部分主要讨论函数与其它比较熟悉的语言(如C/C++)不同的地方

5.1 函数定义
    function 函数名(参数1,参数2...)
    {
        主体;
    }
    函数内可以有return,也可以没有return. 没有return时,函数返回undefined值。
    另外,Javascript是类型宽松的语言,所以,对于函数调用时传递参数,有以下特性:
    ① Javascript不会检测传递的数据是不是函数要求的类型。
        如果参数的数据类型很重要,可以在函数内用typeof运算符进行检测
    ② Javascript不会检测传递给它的参数是否正确。
        如果传递的参数比需求的个数多,多余的值被忽略。
        如果传递的参数比需求的个数少,所忽略的几个参数被赋于undefined值
    ③ 函数内部有个Arguments对象维护着实际传递给函数的参数。
        用它可以实现得到实际传递给函数的参数个数,及参数值。用于实现可变参数函数。

5.2 嵌套函数
    实现如下:

1 img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif function  hypotenuse(a, b)  {
2img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif   function square(x) return x*x; }
3   return Math.sqrt(square(a) + square(b));
4}


5.3 函数参数
    ● 可选参数
        因为不会检测传递给它的参数是否正确,所以,可以忽略到相应的参数来达到可选参数的目的。

1 img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif function  copyPropertyNamesToArray(o,  /**/ /* optional */  a)  {
2    if (!a) a = [];  // If undefined or null, use a blank array
3    // 以上语句也可以这样写 a = a || [];
4    for(var property in o) a.push(property);
5    return a;
6}


    ● 可变参数
        在一个函数体内,标识符arguments具有特殊含义。它是引用arguments对象的一个特殊属性。
        其类似一个数组的对象,可以按照数目获取传递给函数的参数值。
        arguments具有length属性,可以获得实际传递给函数的参数个数
        取得第一个参数值,可以使用argments[0]来获得。

 1 img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif function  max( /**/ /*  */ )
 2 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif {
 3    var m = Number.NEGATIVE_INFINITY;
 4    // Loop through all the arguments, looking for, and
 5    // remembering, the biggest
 6    for(var i = 0; i < arguments.length; i++)
 7         if (arguments[i] > m) 
 8            m = arguments[i];
 9            // Return the biggest
10         return m;
11}


        arguments具有callee属性,引用当前正在执行的函数。
        arguments并非真正的数组,它是一个Arguments对象。
5.4 作为数据的函数
    函数最重要的就是它们能够定义和调用,定义和调用是Javascript和大多数程序语言的语法特性。
    但是,在Javascript中,函数并不只是一种语法,其也可以作为数据来看待。这意味着,可以把函数赋给变量,存储在对象的属性或数组元素中,作为参数传递给函数等。
    例1:函数赋值给变量:

1 img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif function  square(x)  return x*x; }
2 var  a  =  square( 4 );   //  a contains the number 16
3 var  b  =  square;      //  Now b refers to the same function that square does


    例2:存储在对象的属性或数组元素中

1 var  o  =   new  Object;
2 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gifo.square  =   function (x)  return x*x; }         //  function literal
3 =  o.square( 16 );  
4 var  a  =   new  Array( 3 );
5 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gifa[ 0 =   function (x)  return x*x; }


    关于这点,可以参考《Javascript权威指南》8.3节最后例举的一个例子。

5.5 作为方法的函数及this关键字
    对象的方法只不过是对象的一个属性,且该属性引用的是函数类型而已。
    因为函数可以赋给任何变量,可以赋给一个对象的任何属性。
 

1 img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif var  calculator  =   {  // An object literal
2   operand1: 1,
3   operand2: 1,
4img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif   compute: function() {
5       this.result = this.operand1 + this.operand2;
6   }

7}
;
8 calculator.compute();        //  What is 1+1?
9 print(calculator.result);    //  Display the result

    以上,this关键字很重要,任何作方法的函数都被有效的传递了一个隐式的参数,即调用函数的对象(this).

5.6 函数的属性和方法
    函数是Javascript对象的一种特殊类型,typeof运算符用于函数类型时会返回字符串“function”
    既然函数是对象,它就具有属性和方法,就像其它的对象一样。
    ● 属性lenght
        不同于函数体内arguments的length属性,函数自身的length属性是一个只读属性,返回的是函数需要的实际参数数目。
    ● 定义函数自身的属性,具有static属性

 1 //  Create and initialize the "static" variable.
 2 //  Function declarations are processed before code is executed, so
 3 //  we really can do this assignment before the function declaration.
 4 uniqueInteger.counter  =   0 ;
 5
 6 //  Here's the function. It returns a different value each time
 7 //  it is called and uses a "static" property of itself to keep track
 8 //  of the last value it returned.
 9 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif function  uniqueInteger()  {
10    return uniqueInteger.counter++;  // Increment and return our "static" variable
11}


    ● 方法 apply() 和 call()
        这两个方法可以像调用其它方法一样调用函数。第一个参数都是要调用的函数对象,函数体内这一参数是关键字this的值。
        call的剩余参数是传递给要调用的函数的值。
        to the function f() and invoke it as if it were a method of the object o, you could use code like this:
            f.call(o, 1, 2);
        This is similar to the following lines of code:
            o.m = f;
            o.m(1,2);
            delete o.m;
        The apply() method is like the call() method, except that the arguments to be passed to the function are specified as an array:
            f.apply(o, [1,2]);
        For example, to find the largest number in an array of numbers, you could use the apply() method to pass the elements of the array to the Math.max() function:
            var biggest = Math.max.apply(null, array_of_numbers);

目录
相关文章
|
16天前
|
JavaScript
变量和函数提升(js的问题)
变量和函数提升(js的问题)
|
16天前
|
JavaScript
常见函数的4种类型(js的问题)
常见函数的4种类型(js的问题)
10 0
|
16天前
|
JavaScript
写一个函数将N组<>(包含开始和结束),进行组合,并输出组合结果 (js)
写一个函数将N组<>(包含开始和结束),进行组合,并输出组合结果 (js)
9 0
|
27天前
|
自然语言处理 JavaScript 网络架构
js开发:请解释什么是ES6的箭头函数,以及它与传统函数的区别。
ES6的箭头函数以`=&gt;`定义,简化了函数写法,具有简洁语法和词法作用域的`this`。它无`arguments`对象,不能用作构造函数,不支持`Generator`,且不改变`this`、`super`、`new.target`绑定。适用于简短表达式,常用于异步编程和高阶函数。
17 5
|
1月前
|
JavaScript 前端开发 网络架构
JavaScript 谈谈对箭头函数的理解及其与普通函数的区别。
JavaScript 谈谈对箭头函数的理解及其与普通函数的区别。
17 1
|
1月前
|
前端开发 JavaScript 数据处理
在JavaScript中,什么是异步函数执行的例子
在JavaScript中,什么是异步函数执行的例子
10 0
|
1月前
|
JavaScript
JS封装节流函数
JS封装节流函数
15 0
|
1月前
|
JavaScript 前端开发
javascript箭头函数
javascript箭头函数
|
1月前
|
JavaScript 小程序
微信小程序 wxml 中使用 js函数
微信小程序 wxml 中使用 js函数
67 0
|
1月前
|
JavaScript 前端开发
JavaScript函数科里化
JavaScript函数科里化