简体   繁体   中英

My code broke jsbin and repl.it?

while (re)doing this tutorial, http://reactivex.io/learnrx/ , I find that both repl.it and jsbin.com reject functional javascript like this.

movieLists.
        concatMap(function(category) {
            return category.videos.map(function(video){

                return video.boxarts
                    .reduce(function(acc,cur){
                        return (acc.width*acc.height)<(cur.width*cur.height) ? acc:cur
                    }).map(function(boxart){
                        return {id:video.id, title:video.title}
                    });


            })
        })

results in

"TypeError: video.boxarts.reduce(...).map is not a function
    at dihuwo.js:105:24
    at Array.map (native)
    at dihuwo.js:100:36
    at dihuwo.js:24:14
    at Array.map (native)
    at Array.concatMap (dihuwo.js:18:3)
    at dihuwo.js:99:3
    at dihuwo.js:113:3
    at https://static.jsbin.com/js/prod/runner-3.35.5.min.js:1:13616
    at https://static.jsbin.com/js/prod/runner-3.35.5.min.js:1:10537"

why does the code here on jsbin fail, while the repl here accepts it as legit?

What silly thing am I missing? I repeat... the code is passing on the tutorial page. It's just impossible so far to test on any other repl.

PS All helper functions, such as concatMap, are defined above this code, so that's not the problem.

It does not work because in that tutorial they override Array.prototype.reduce .

Their implementation works differently to the standard one and it always returns an array.

Generally it's a bad practice to do so and must be avoided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM