美文网首页JavaScript 使用记录
JavaScript Function 类型

JavaScript Function 类型

作者: 赵者也 | 来源:发表于2017-12-08 16:02 被阅读6次

    JavaScript 的 Function 类型实际上是对象。每个函数都是 Function 类型的实例,而且而且与其他类型一样都有属性和方法。

    由于函数是对象,因此函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定。

    两种定义形式:

    // 形式一
        function sum1(num1, num2) {
            return num1 + num2;
        }
    // 形式二
        var sum2 = function(num1, num2) {
            return num1 + num2;
        }
    

    因为函数名仅仅是指向函数的指针,所以可以我们可以像下面这样使用它:

        function sum1(num1, num2) {
            return num1 + num2;
        }
    
        Component.onCompleted: {
            var sum2 = sum1;
            var result = sum2(1, 2);
            console.log("result: \t", result);
        }
    
    函数内部属性

    在函数内部,有两个特殊的对象: arguments 和 this。其中,arguments 是一个类数组对象,包含着传入函数中的所有参数。虽然 arguments 的主要用途是保存函数参数,但是这个对象还有一个名叫 callee 的属性,该属性是一个指针,指向拥有这个 arguments 对象的函数。

    下面是一个阶乘函数:

        function factorial(num) {
            if (num <=1 ) {
                return 1;
            } else {
                return num * factorial(num - 1);
            }
        }
    

    上面这段递归将函数的执行与函数名耦合在了一起。为了消除这种紧密的耦合可以像下面这样使用 arguments.callee

        function factorial(num) {
            if (num <=1 ) {
                return 1;
            } else {
                return num * arguments.callee(num - 1);
            }
        }
    
        Component.onCompleted: {
            var otherFactorial = factorial;
            var result = otherFactorial(5);
            console.log("result: \t", result);
        }
    

    这样,无论调用时使用的是什么函数名,都可以保证正常完成递归调用。

    函数内的另一个特殊对象是 this,其行为与 Java 和 C# 中的 this 大致相似。换句话说,this 引用的是函数据以执行的环境对象 —— 或者也可以说是 this 值。下面是一段偏向 QML 形式的 JavaScript 代码:

        property string color: "red"
        property var object: new Object
    
        function sayColor() {
            console.log("color: \t", this.color);
        }
    
        Component.onCompleted: {
            object.color = "blue";
            sayColor();
            object.sayColor = sayColor;
            object.sayColor();
        }
    

    上述代码输出结果:

    输出结果

    一定要牢记,函数的名字仅仅是一个包含指针的变量而已。因此,即使是在不同的环境中执行,全局的 sayColor() 函数与 object.sayColor() 指向的仍然是同一个函数。

    函数属性和方法

    每个函数都包含两个属性:length 和 prototype。其中,length 属性表示函数希望接收的命名参数的个数。如下面实例:

        function sayHi(name) {
            console.log("Hi,", name);
        }
    
        function sum(num1, num2) {
            return num1 + num2;
        }
    
        function getColor() {
            return "red";
        }
    
        Component.onCompleted: {
            console.log("sayHi length: \t", sayHi.length);
            console.log("sum length: \t", sum.length);
            console.log("getColor length: \t", getColor.length);
        }
    

    输出结果是:

    输出结果

    每个函数都包含两个非继承而来的方法 apply() 和 call()。

    apply() 和 call() 其实差不多,唯一不同的是,使用 call() 时,传递给函数的参数必须逐个列举出来。

    下面是使用实例:

        function sum(num1, num2) {
            return num1 + num2;
        }
    
        function callSum1(num1, num2) {
             // call() 方法就不能这么用了
            return sum.apply(this, arguments);
        }
    
        function callSum2(num1, num2) {
            return sum.apply(this, [num1, num2]);
        }
    
        function callSum(num1, num2) {
            return sum.call(this, num1, num2);
        }
    
        Component.onCompleted: {
            console.log("callSum1: \t", callSum1(1, 2));
            console.log("callSum2: \t", callSum2(2, 3));
            console.log("callSum: \t\t", callSum(3, 4));
        }
    

    输出结果:

    输出结果

    事实上,传递参数并非 apply() 和 call() 真正的用武之地,它们真正强大的地方是能够扩充函数赖以运行的作用域。下面是一个实例:

    Window {
        id: window
    
        property string color: "red"
        property var o: new Object()
    
        function sayColor() {
            console.log("color: \t", this.color);
        }
    
        Component.onCompleted: {
            o.color = "blue";
    
            sayColor.call(this);
            sayColor.call(window);
            sayColor.call(o);
        }
    }
    

    上述代码的输出:

    输出结果

    从上例中我们可以看出使用 apply() 和 call() 的好处是对象不需要与方法有任何耦合关系。

    函数还提供了 bind() 方法,这个方法会创建一个函数实例,其 this 值会被绑定到传给 bind() 函数的值。例如:

    Window {
        id: window
    
        property string color: "red"
        property var o: new Object;
    
        function sayColor() {
            console.log("color: \t", this.color);
        }
    
        Component.onCompleted: {
            o.color = "blue";
    
            var objectSayColor = sayColor.bind(o);
            objectSayColor();
        }
    }
    

    将会输出:

    输出结果

    在这里,sayColor 调用 bind 并传入对象 o,创建了 objectSayColor 函数。objectSayColor 函数的 this 值等于 o,因此即使是在全局作用域中调用这个函数,也会看到输出 “blue”。

    相关文章

      网友评论

        本文标题:JavaScript Function 类型

        本文链接:https://www.haomeiwen.com/subject/ymosixtx.html