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}]
}
})()