zoukankan      html  css  js  c++  java
  • javascript中return function与return function()的区别

    参考https://stackoverflow.com/questions/7629891/functions-that-return-a-function-javascript

    问题:唯一的区别是return中的函数是否带括号

    输入:

    function a() {
    
        alert('A!');
    
        function b(){
            alert('B!'); 
        }
    
        return b();
    }
    
    var s = a();
    alert('break');
    s();

    输出:

    A!
    B!
    break

    输入:

    function a() {
    
        alert('A!');
    
        function b(){
            alert('B!'); 
        }
    
        return b;
    }
    
    var s = a();
    alert('break');
    s();

    输出:

    A!
    break
    B!

    回答1:

    Assigning a variable to a function (without the parenthesis) copies the reference to the function. Putting the parenthesis at the end of a function name, calls the function, returning the functions return value.

    http://jsfiddle.net/kaleb/Las6w/

    function a() {
        alert('A');// A未在此作用域定义
    }
    //alerts 'A', returns undefined
    
    function b() {
        alert('B');
        return a; //返回的是一个函数
    }
    //alerts 'B', returns function a
    
    function c() {
        alert('C');
        return a();//返回函数执行结果
    }
    //alerts 'C', alerts 'A', returns undefined
    
    alert("Function 'a' returns " + a());
    alert("Function 'b' returns " + b());
    alert("Function 'c' returns " + c());

    回答2:

    Returning the function name without () returns a reference to the function, which can be assigned as you've done with var s = a()s now contains a reference to the function b(), and calling s() is functionally equivalent to calling b().

    // Return a reference to the function b().
    // In your example, the reference is assigned to var s
    return b;

    Calling the function with () in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();, but instead of assigning the return value of b() you are returning it from the calling function a(). If the function b() itself does not return a value, the call returns undefined after whatever other work is done by b().

        
    Returning the function name without () returns a reference to the function, which can be assigned as you've done with var s = a(). s now contains a reference to the function b(), and calling s() is functionally equivalent to calling b().
    
    // Return a reference to the function b().
    // In your example, the reference is assigned to var s
    return b;
    Calling the function with () in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();, but instead of assigning the return value of b() you are returning it from the calling function a(). If the function b() itself does not return a value, the call returns undefined after whatever other work is done by b().
    
    // Execute function b() and return its value
    return b();
    // If b() has no return value, this is equivalent to calling b(), followed by
    // return undefined;

     回答3:

    return b(); calls the function b(), and returns its result.

    return b; returns a reference to the function b, which you can store in a variable to call later.

  • 相关阅读:
    关于R.styleable的问题
    Android游戏开发学习(5)--实现Button悬浮于与SurfaceView之上
    android悬浮按钮(Floating action button)的两种实现方法
    LinearLayout属性android:orientation
    Android详细的对话框AlertDialog.Builder使用方法
    SQLiteDatabase中query、insert、update、delete方法参数说明
    android原始sqlite中query的复杂用法
    Android加载手机磁盘上的资源---decodeFile方法的使用
    Laravel 5.2分页--怎么在一个页面实现两个以上的列表分页,互不影响?
    琐碎注意点⚠️
  • 原文地址:https://www.cnblogs.com/ys-wuhan/p/7026025.html
Copyright © 2011-2022 走看看