我试图以这种方式在其函数之外获取snapAuthor变量的值,但是控制台告诉我没有找到它!
<b:tag name='script' type='text/javascript'>
function OuterFn(){
var snapAuthor = AuthorsInfo.filter(function(a){
return a.name==='<data:title/>'
})[0];
if(snapAuthor!==undefined){
snapAuthor.provided=true;
<b:loop values='data:links' var='link'>
<b:if cond='data:link.name contains "-ad"'>
snapAuthor['<data:link.name/>']='<data:link.target.jsEscaped/>';
<b:else/>
<b:switch var='data:link.name'>
<b:case value='rank'/>snapAuthor.rank='<data:link.target.escaped/>';
<b:case value='about'/>snapAuthor.about='<data:link.target.escaped/>';
<b:default/>snapAuthor.links['<data:link.name/>']='<data:link.target/>';
</b:switch>
</b:if>
</b:loop>
}
var getSlices = function(){
return snapAuthor;
}
var slice = function(){
snapAuthor++;
}
};
var outerFn = new OuterFn();
console.log(outerFn.getSlices);// undefined
console.log(outerFn.slice);// undefined
</b:tag>发布于 2020-04-03 19:17:38
在JavaScript中,变量的作用域是函数。因此,如果此变量是在函数中定义的,则无法在此函数之外访问它。您应该在其他地方定义该变量。
发布于 2020-04-03 19:23:44
您应该将函数OuterFn函数外部的变量定义为:
var snapAuthor;然后在函数中只需在其中填充数据
function OuterFn(){
snapAuthor = AuthorsInfo.filter(function(a){这应该能起到作用。
https://stackoverflow.com/questions/61010417
复制相似问题