Javascript变量赋值从函数链

javascript variable assignment from chain of functions

本文关键字:函数 赋值 变量 Javascript      更新时间:2023-09-26

试着让这句话简洁一点:

    //first named function
    function tryMe(x){
        tryMeAgain(x);
    };
    //second named function
    function tryMeAgain(y){
        return y;
    };
    //assign the result of first function (which will actually be the result of the second function) to a var
    var testTry = tryMe('worth a shot');
    console.log(testTry); //undefined! But I would like 'testTry' to return 'worth a shot'

我有两个问题:

  1. 为什么?
  2. 我如何正确分配'testTry' ?

编辑如下:所以所有的回应都是有意义的,我认为我的问题可能在我的代码的其他地方。我试图简化这个问题,但可能忽略了另一个难题。我附上了一个新版本,希望你们都能有所启发:

  var runtime = (function(){
        var $jq = jQuery.noConflict(true);
        function Runtime(){
            this.$jq = $jq;
        }
        Runtime.prototype.method1 = function( _value, _callback ){
            setTimeout(function(){ console.log('dependency1_resolved');
                _callback.apply(this, [{valIs:_value}]);
            }.bind(this), (Math.random() * 1000));
        };
        Runtime.prototype.method2 = function( _value, _callback ){
            var self = this;
            setTimeout(function(){ console.log('dependency2_resolved');
                _callback.apply(self, [{differntValIs:3}]);
            }.bind(this), (Math.random() * 1000));
        };
        Runtime.prototype.method3 = function( _value, _callback ){
            setTimeout(function(){ console.log('dependency3_resolved');
                _callback.apply(this, [{valIs:_value['differntValIs'] *= 4}]);
            }.bind(this), (Math.random() * 1000));
        };
        return new Runtime();
    })();

  runtime.initialize(function( $ ){
    function firstCalc(firstInput){
        return runtime.method1(firstInput,secondCalc);
    };
    function secondCalc(secondInput){
        return runtime.method2(secondInput, thirdCalc);
    };
    function thirdCalc(thirdInput){
        return runtime.method3(thirdInput, fourthCalc);
    };
    function fourthCalc(ourResult){
        //console.log( ourResult );
        return ourResult;
    };
    var _value = firstCalc(4); //this is undefined!!
});

您将返回tryMeAgain而不是tryMe方法的值,因此返回的默认值是undefined

//first named function
function tryMe(x) {
  return tryMeAgain(x);//need to return the value returned by tryMeAgain to the caller of tryMe
};
//second named function
function tryMeAgain(y) {
  return y;
};
//assign the result of first function to a var
var testTry = tryMe('worth a shot');
snippet.log(testTry);
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

你的整个代码是正确的,只是有一些小错误。

  function tryMe(x){
             tryMeAgain(x);
         };

在这里你只是调用了函数但没有返回调用它的人的值,那么肯定是

 var testTry = tryMe('worth a shot');

undefined

改为

function tryMe(x){
     return  tryMeAgain(x);
 };

编辑

 function firstCalc(firstInput){
        return runtime.method1(firstInput,secondCalc);
    };

你的firstCalc调用secondCalc和secondCalc调用thirdCalc,但你错过的是你的firstCalcsecondCalcthirdCalc是函数和你传递这些值作为变量没有任何值调用它们作为函数。

function firstCalc(firstInput){
        var secondCalc = secondCalc(thiValueYouWantToCalculate);
            return runtime.method1(firstInput,secondCalc);
        };

除非特别说明,函数默认返回值为undefined。这就是代码中发生的事情,testTry返回undefined

让我们遵循以:

开头的函数调用

var _value = firstCalc(4);

firstCalc(4)的结果是什么?好吧,firstCalc()返回runtime.method1()的结果。

运行的结果是什么?secondCalc method1(4日)?它是未定义的,因为runtime.method1()只是执行setTimeout来稍后调用secondCalc(),然后返回。没有显式的返回语句,因此默认行为是返回undefined。

这就是你的初始表达式"var _value = firstCalc(4)"结束的地方。

settimeout导致其他函数在随机延迟后被调用,实际上在fourthCalc()中,变量ourResult应该是{valIs: 12},但这发生在稍后,在settimeout导致各种calc函数被调用之后。此外,在fourthCalc中的"返回我们的结果"返回我们的结果到…谁?没有人;没有将fourthCalc的结果赋值给任何对象

试试这个:

var _value = firstCalc(4); //this is undefined!!
setTimeout(function() { console.log(_value); }, 5000);