javascript 将从javascript函数返回的值分配给变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14718463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign value returned from a javascript function to a variable
提问by marky
I have a function that returns a string, and I need to call the function several times and put the result in a variable.
我有一个返回字符串的函数,我需要多次调用该函数并将结果放入一个变量中。
I have this:
我有这个:
function GetValue() {
... Do Something ...
console.log(string); // displays 'string'
return string;
}
And I'm trying to assign the output of the function to a variable in another function. Currently I'm doing this:
我试图将函数的输出分配给另一个函数中的变量。目前我正在这样做:
var theString = GetValue();
console.log(theString); // displays 'undefined'
What I don't understand is the console output in the function is displaying the value, but it's not after assigning the variable to the output of the function.
我不明白的是函数中的控制台输出正在显示值,但不是在将变量分配给函数的输出之后。
Obviously this isn't the way to assign the output of a function to a variable. So how do I do that?
显然,这不是将函数的输出分配给变量的方法。那我该怎么做呢?
[ADDITIONAL INFO]
[附加信息]
Apparently my attempt at being brief in my sample code only served to cause confusion.
显然,我试图在我的示例代码中保持简短只会引起混乱。
Here is the full function that I need to call several times from elsewhere in the javascript file:
这是我需要从 javascript 文件中的其他地方多次调用的完整函数:
/*
* Build URL link
*/
function BuildUrlTargetFolder() {
// Get the current url and remove the 'zipCodes/branchAdmin' part of it for the branch url link
var urlArray = window.location.href.split('/'),
targetLength = urlArray.length - 3,
i,
targetFolder = '';
for (i = 0; i < targetLength; i++) {
// Only add a slash after the 'http:' element of the array
if (i === 0) {
targetFolder += urlArray[i];
} else {
targetFolder += '/' + urlArray[i];
}
}
console.log('targetFolder: ' + targetFolder); // console output is the current url minus two "levels"
return targetFolder;
}
Here's one of the places that the function needs to be used:
下面是需要使用该函数的地方之一:
var targetFolder = BuildUrlTargetFolder();
console.log('targetFolder: ' . targetFolder); // console output: "undefined"
// If the url has a value, set the href attribute for the branch link otherwise hide the url
if (data['url'] !== '') {
$('a#branchLink').attr('href', targetFolder + '/index.php/coverage-area/' + data['url']).show();
} else {
$('a#branchLink').attr('href', '#').hide();
}
So, having said that, how do I get the string from the function assigned to the calling code's variable?
那么,话虽如此,我如何从分配给调用代码变量的函数中获取字符串?
采纳答案by lawnsea
The problem is this line:
问题是这一行:
console.log('targetFolder: ' . targetFolder); // console output: "undefined"
The .
should be a +
.
本.
应该是一个+
。
As written, your code is equivalent to doing this:
如所写,您的代码等效于执行以下操作:
console.log('targetFolder'.targetFolder);
In other words, it evaluates to the targetFolder
property of the string 'targetFolder' (coerced to a String
instance) - which is undefined.
换句话说,它计算targetFolder
字符串 'targetFolder'(强制为String
实例)的属性- 这是未定义的。