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

  • SEARCH
  • Home
  • 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 7610541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:26:32+00:00 2026-05-31T01:26:32+00:00

I’ve been playing around with Backbone.js and ran into something a bit unexpected. It

  • 0

I’ve been playing around with Backbone.js and ran into something a bit unexpected. It seems that toJSON() (and as a result of attributes) will only return a JSON object if I explicitly set the attributes. Here’s the issue:

  1. Extend/inherit a sample object with some data
  2. grab the resulting constructor
  3. instantiate/new an instance
  4. call the toJSON() function

I expected the call to return my object with all attributes (inherited and bound), but instead I got back an empty {} object.

Here’s the sample code:

var TestModelConstructor = Backbone.Model.extend({ name: "test", num: 483 });
var testinstance = new TestModelConstructor;

console.log(testinstance.name); //prints: test
console.log(testinstance.num);  //prints: 483
console.log(JSON.stringify(testinstance.toJSON())); //prints: {}
console.log(testinstance.attributes); //again empty object

testinstance.newattr = "adding new attribute";
console.log(testinstance.attributes); //{}  ????

//either set explicitly by calling "set" or via constructor/initialize
testinstance.set({ label: "i can setz attributez" });
console.log(JSON.stringify(testinstance.toJSON())); 
                                      //{"label":"i can setz attributez"}
console.log(testinstance.attributes); //{"label":"i can setz attributez"}

Either this behavior is strange or I missed something in the documentation.

  • 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-05-31T01:26:33+00:00Added an answer on May 31, 2026 at 1:26 am

    I think you should be using the defaults parameter to the extend function to set the defaults. Parameters passed to the extend function will be members on the class rather than attributes, which is what toJSON uses.

    var TestModelConstructor = Backbone.Model.extend({
        defaults : { name: "test", num: 483 }
    });
    var testinstance = new TestModelConstructor;
    ....
    

    See the defaults documentation for more details.

    To be clear, the attributes Backbone.Model uses are stored in the attributes member of the class rather than on the actual class itself. This avoids confusing actual model data with functions that you call on the model and settings that you use to configure the model. It’s these functions and settings that you’re creating/setting when you pass arguments to the Model.extend function.

    So when you’re trying to set testinstance.newattr you should actually use testinstance.attributes.newattr instead. This is what the .set method is actually doing behind the scenes, so I’d recommend just using .set.

    Again, the attributes documentation has more details

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.