Javascript变量赋值从函数链
javascript variable assignment from chain of functions
试着让这句话简洁一点:
//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'
我有两个问题:
- 为什么?
- 我如何正确分配'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,但你错过的是你的firstCalc, secondCalc和thirdCalc是函数和你传递这些值作为变量没有任何值调用它们作为函数。
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);
- 将ECMAScript 6析构函数赋值(ES2015)重构为旧版本的javascript
- 将函数赋值给 javascript 中的元素
- 如何在 jQuery 中的变量中调用函数赋值
- 在 JavaScript 中将函数赋值给变量
- CoffeeScript-如何将长的析构函数赋值分解为多行
- 循环内部的jQuery mouseover()函数赋值不正确
- 从Java函数向JS/JQuery函数赋值/传递值
- 原型继承上的构造函数赋值
- 为函数赋值并在单独的按钮jquery上运行
- 在javascript中,这种函数赋值给变量是有用的
- 将非匿名函数赋值给变量
- 有没有办法使构造函数赋值更简洁(和简单)?
- 如何将函数赋值给对象
- 将函数赋值给变量并运行它自己
- 如何将返回的Javascript函数赋值给PHP变量并在SQL查询中使用
- 将已定义的函数赋值给具有一组参数的变量
- Javascript在函数赋值后未定义
- Javascript将函数赋值给现有变量
- 将构造函数赋值给某物是什么意思?
- 将php函数赋值给动态创建的对象