admin 管理员组文章数量: 1086019
so i have a variable which as this point has a static definition, i want it to get now the value dynamically from an if-else
statement but the above mentioned error pops out:
var x = [{a:b},{c:d},{e:f}]
and i want to change it to get the value from a function:
var x = function(){
if(true){
return [{a:b},{c:d},{e:f}]
}else{
return [{f:g},{h:i}]
}
}
But I get an error x.slice is not a function
Can what am i doing wrong? Looked around but cant manage to fix this... isn't it possible?
so i have a variable which as this point has a static definition, i want it to get now the value dynamically from an if-else
statement but the above mentioned error pops out:
var x = [{a:b},{c:d},{e:f}]
and i want to change it to get the value from a function:
var x = function(){
if(true){
return [{a:b},{c:d},{e:f}]
}else{
return [{f:g},{h:i}]
}
}
But I get an error x.slice is not a function
Can what am i doing wrong? Looked around but cant manage to fix this... isn't it possible?
2 Answers
Reset to default 4x
is a function, so you're trying to call .slice()
on a function. You want to call x()
and use the return value, then slice that:
x().slice(1);
x is a function so it does not have slice you should run it before
var x = (function(){
if(true){
return [{a:b},{c:d},{e:f}]
}else{
return [{f:g},{h:i}]
}
})()
本文标签: avoid slice is not a function (javascript)Stack Overflow
版权声明:本文标题:avoid .slice is not a function (javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744036528a2522466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论