最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

avoid .slice is not a function (javascript) - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question asked Nov 10, 2014 at 10:08 Bogdan M.Bogdan M. 2,1917 gold badges31 silver badges54 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

x 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}]
    }
})()
发布评论

评论列表(0)

  1. 暂无评论