Understanding the result returned from splicing in CoffeeScript

I am using CoffeeScript along with JS splicing feature. My understanding of the JS splicing feature is that it should return objects that have been spliced ​​and modify the original array. This seems to work fine with simple arrays, but when I start adding objects to the array everything breaks down. Below is a simplified example with comments:

And link code

#Class that will go in array
class Thing
  do: ->
    alert "Hi"

a = new Thing
b = new Thing

arr = []

arr.push(a)
arr.push(b)

arr[0].do()  # this works

result = arr.splice(0,1)
alert result.do()  # this does not work

      

Does splicing do something that makes it unusable? If anyone has any idea of ​​the reason why this is happening and / or fix it, I would be very grateful,

+3


source to share


2 answers


Array.splice()

returns an array deleted items; since it can remove several by the second parameter:

Because of this, you must use alert result[0].do();



Working example: http://jsfiddle.net/Cjtaa/

+4


source


splice

returns array

.

So, you need to do:



result = arr.splice(0,1)
alert result[0].do() 

      

+1


source







All Articles