Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:04:29+00:00 2026-06-04T00:04:29+00:00

I’m iterating over some api data and mapping the API’s data to Coffeescript objects.

  • 0

I’m iterating over some api data and mapping the API’s data to Coffeescript objects. What’s puzzling me is why just part of that data disappears between these two console.log statements:

items = (for item in @apiFeedPage.items
         fi = new API.FeedItem(item, @apiFeedPage.instance_url)
         console.log fi.comments.comments
         window.API.itemHash[fi.id] = fi  #save for later usage
         fi )       
console.log items[0].comments.comments

In the above, the first console.log outputs the comments I expect: fi.comments.comments is equal to an array of Comment objects (Comments are a property on a FeedItem) In the second console.log statement, the comments objects are there but are not assigned – as if the Comment constructor was run on a API response with no comments.

The constructor looks like this:

class API.FeedItem extends API.FeedComponent 
  # instance vars
  comments: {}

  constructor: (data, instance_url) ->
    super(data, instance_url)
    @parent = new API.User( data.parent )
    @comments.comments = (new API.Comment(api_comment) for api_comment in data.comments.comments)   
    @comments.total = data.comments.total
    @comments.nextPageUrl = data.comments.nextPageUrl

And I’ve confirmed that inside the constructor, the @comments.comments is properly assigned, which is what you’d expect since the first console.log statement has the expected objects. The first block of code above sits in a Ajax callback function using a fat arrow, so my initial suspicion that this had to do with losing the context of “this” doesn’t seem to apply, and since all the other expected data inside FeedItem is there…

Any ideas on why the items[0].comments.comments is equal to [] in the second statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:04:31+00:00Added an answer on June 4, 2026 at 12:04 am

    I would guess that the last data.comments.comments inside your API.FeedItem constructor is empty. That would cause @comments.comments to be []. But then you’d ask:

    Why would the last data.comments.comments value change items[0]?

    The answer is simple, this:

    class API.FeedItem extends API.FeedComponent 
      # instance vars
      comments: {}
    

    creates a single comments = { } object that is attached to the API.FeedItem prototype and thus is shared by all instances of API.FeedItem; in other words, the comment is a lie, that’s not an instance variable.

    Consider this simplified analogue to your situation:

    class AFI
      comments: { }
      constructor: (n) ->
        @comments.comments = [ 1 .. n ]
    
    fitems = [ 1 .. 4 ]
    items = (new AFI(i) for i in fitems)
    console.log item.comments for item in items
    

    You’re probably expecting to see [1], [1,2], [1,2,3], [1,2,3,4] come out but you’ll see four identical [1,2,3,4]s instead: http://jsfiddle.net/ambiguous/hf9zL/

    If you fix your class to initialize @comments per instance:

    class AFI
      constructor: (n) ->
        @comments = { }
        @comments.comments = [ 1 .. n ]
    

    Then you’ll see the [1], [1,2], [1,2,3], [1,2,3,4] that you’re expecting.

    Demo: http://jsfiddle.net/ambiguous/qs9Zy/

    You could even throw a

    console.log items[0].comments.comments == items[1].comments.comments
    console.log [1] == [1]
    

    in to see that the arrays are, in fact, the same array; the extra [1] == [1] comparison is to prove to you that == is actually comparing the objects rather than their content (i.e. CoffeeScript’s == is JavaScript’s ===).

    A rule of thumb: don’t try to define instance variables in the class definition, define them in the constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.