Make Javascript local variable to global for recursive loops - Stack Overflow

I have a recursive function which has a local variable.It calls itself on specific condition.The loca

I have a recursive function which has a local variable. It calls itself on specific condition. The local variable needs to be updated, but every call it creates a new local variable specific to the current function scope. How can i reach the local variable for access all recursive loop and not to create a new one? Something like __Callee.varname?

The code is:

var addAttribute = function(object,elem)
{
    var attributes = [];

    // only attribute without values
    if ( object instanceof Array )
    {
        for ( var value in object )
        {
            attributes.push(object[value]);
        }
    }
    // attribute with values
    else if ( object instanceof Object )
    {
        for ( var key in object )
        {
            if ( object[key] instanceof Array )
            {
                addAttribute(object[key],elem);
            }
            else
            {
                attributes.push(key+'=\''+object[key]+'\'');
            }
        }
    }
    // Only one attribute
    else if ( typeof object === 'string' )
    {
        attributes.push('\''+object+'\'');
    }
    // Invalid parameter
    else
    {
        console.log('Invalid parameter: '+typeof object);
    }

    console.log('<'+elem+' '+attributes.join(' ').toString()+' />');

}

I do not want to make variable to global because of using this name in other functions and global scope already.

I have a recursive function which has a local variable. It calls itself on specific condition. The local variable needs to be updated, but every call it creates a new local variable specific to the current function scope. How can i reach the local variable for access all recursive loop and not to create a new one? Something like __Callee.varname?

The code is:

var addAttribute = function(object,elem)
{
    var attributes = [];

    // only attribute without values
    if ( object instanceof Array )
    {
        for ( var value in object )
        {
            attributes.push(object[value]);
        }
    }
    // attribute with values
    else if ( object instanceof Object )
    {
        for ( var key in object )
        {
            if ( object[key] instanceof Array )
            {
                addAttribute(object[key],elem);
            }
            else
            {
                attributes.push(key+'=\''+object[key]+'\'');
            }
        }
    }
    // Only one attribute
    else if ( typeof object === 'string' )
    {
        attributes.push('\''+object+'\'');
    }
    // Invalid parameter
    else
    {
        console.log('Invalid parameter: '+typeof object);
    }

    console.log('<'+elem+' '+attributes.join(' ').toString()+' />');

}

I do not want to make variable to global because of using this name in other functions and global scope already.

Share Improve this question edited Oct 13, 2016 at 9:55 Chris Charles 4,44619 silver badges31 bronze badges asked Oct 13, 2016 at 9:54 Németh PéterNémeth Péter 1512 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Use a closure

function fn() {
    function recursiveFunction() {
        // do something with x
        recursiveFunction();
    }
    var x = 0;
    recursiveFunction();
}

The usual thing is to pass it into the function, possibly optionally:

var addAttribute = function(object,elem, attributes) {
    attributes = attributes || [];
    // ....

Then when calling it recursively, pass in the third argument:

addAttribute(object[key], value, attributes);

Here's a much simplified example demonstrating:

function foo(num, array) {
  array = array || [];
  array.push(num);
  console.log("Pushed " + num + ", array = " + JSON.stringify(array));
  if (num < 5) {
    foo(num + 1, array);
  }
}
foo(1);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745311033a4621999.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信