最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Assign value returned from a javascript function to a variable - Stack Overflow

programmeradmin6浏览0评论

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:

/*
* 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?

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:

/*
* 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?

Share Improve this question edited Feb 6, 2013 at 0:24 marky asked Feb 5, 2013 at 22:48 markymarky 5,08818 gold badges63 silver badges110 bronze badges 6
  • Works for me: jsfiddle/zzzHz – gen_Eric Commented Feb 5, 2013 at 22:55
  • Do you have any errors in your console? How is string being created? Is GetValue in scope? – gen_Eric Commented Feb 5, 2013 at 22:55
  • Do you want the output of the function GetValue() to be the type of string? In general, I don't think you should name a variable the same as a type. Consider changing the variable name 'string'. – Josh C. Commented Feb 5, 2013 at 22:56
  • 1 Also, do you have a var declaration somewhere in the ...Do Something... section? Otherwise, the variable would be undefined if it is never declared and never assigned. – Josh C. Commented Feb 5, 2013 at 22:57
  • @Josh, see "Additional Info" section I added to my OP for clarification. – marky Commented Feb 6, 2013 at 0:25
 |  Show 1 more ment

1 Answer 1

Reset to default 3

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.

发布评论

评论列表(0)

  1. 暂无评论