1
我有一个递归函数,它有一个局部变量。 它在特定条件下自行调用。 局部变量需要更新,但每次调用都会创建一个特定于当前函数作用域的新局部变量。 我怎样才能到达本地变量访问所有递归循环,而不是创建一个新的? 类似__Callee.varname?使Javascript成为全局递归循环的局部变量
的代码是:
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()+' />');
}
我不想让变量,因为全球其他功能和全球范围内已经使用这个名字。
它不会工作,因为调用函数属性不能被设置为调用的函数属性。 –
@NémethPéter:对不起,我不明白“呼叫者功能属性”或“被调用的功能属性”是什么意思。以上将使用一个共享数组,就像Nina答案中的单个共享'x'一样。 –