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 8232115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:43:21+00:00 2026-06-07T17:43:21+00:00

I want to built a Backbone.Model like object/class. I looked through the Backbone docs

  • 0

I want to built a Backbone.Model like object/class.

I looked through the Backbone docs and found that they create a inheritable object/class with its inheritable attributes, methods in two steps:

I. Create a function with some attributes

var Model = Backbone.Model = function(attributes, options) {
    var defaults;
    attributes || (attributes = {}); //?
    if (options && options.parse) attributes = this.parse(attributes); //?
    if (defaults = getValue(this, 'defaults')) {
      attributes = _.extend({}, defaults, attributes); // ?
    }
    if (options && options.collection) this.collection = options.collection;
    this.attributes = {};
    this._escapedAttributes = {};
    this.cid = _.uniqueId('c');
    this.changed = {};
    this._silent = {};
    this._pending = {};
    this.set(attributes, {silent: true});

    this.changed = {};
    this._silent = {};
    this._pending = {};
    this._previousAttributes = _.clone(this.attributes);
    this.initialize.apply(this, arguments);
};

II. use underscore’s extend to give it some functions

_.extend(Model.prototype, Events, { // Events?
   changed: null,

   _silent: null,

   _pending: null,

   idAttribute: 'id',

   initialize: function(){},

   toJSON: function(options) {
       return _.clone(this.attributes);
   }

   // other Model methods...
};

I have some questions about this behaviour:

  1. What does line 3-4 in the first part do
  2. What happens in line 6?
  3. Why do we overgive “_.extend” the Events object, what else could I give as parameter?

Is there anything else I have to pay attention about?

Regards

  • 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-07T17:43:23+00:00Added an answer on June 7, 2026 at 5:43 pm

    Line 3, attributes || (attributes = {}); is an example of short circuit evaluation.
    If attributes has a falsy valye, then javascript will evaluate the second part of the OR expression. That part assigns attributes a value of {}, an empty object. The end result is, if attributes is null or undefined (typical case) then, assign attributes an empty object.

    That line is equivalent to saying:

    if (attributes == false) { // attributes can be any value that evaluates to false
                               // attributes could be null, undefined, false, 0 etc
       attributes = {};
    }
    

    Same case with line 4, if (options && options.parse) attributes = this.parse(attributes);

    If an options object exists, ie its not null or undefined, and the options object has a valid property called parse, then assign attributes the value of this.parse(attributes);

    EDIT 1:

    The _.extend lines can be explained by the Underscore.js documentation.

    Its a standard pattern to create a new object that will contain properties of all passed in objects. In the first example attributes = _.extend({}, defaults, attributes); // ? the pattern is used to override the default values with what ever may have been passed. You would see this pattern followed in most plugins that allow you to pass in an options object. If you dont pass in anything, the default values will be used. If you pass in only a few properties, the remaining will get their values from the defaults.

    http://api.jquery.com/jQuery.extend/ is NOT the exact same thing. But its very similar. Their documentation is much better. You will understand the concept better.

    EDIT 2:

    The Backbone Events class has some event handling related methods. The Backbone Model class also needs to be able to make use of event handling. It needs to raise events to tell the view to re-render itself, it needs to listen to events from the dom when the view changes the underlying data. This event handling functionality could either have been re-written from scratch when defining the Model class, or the Model class could be extended to get these methods from the Events class. That latter is what is happening.

    If you read the documentation for the $.extend or _.extend, you will realize that the Model class is being extended not just with the properties under Events, but also with other properties like toJSON and initialize etc…

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

Sidebar

Related Questions

I want to pass a pre-built Map to multiple threads that are going to
I want to export the assemblies built in my project. But where are they
I want to create a purpose built device capable of running a java vm
I think I want to use pythons built in calendar module to create an
I use some libraries that I don't want built as part of every project
I need a little help. I want to built a user interactive site that
I want to built a calendar that displays events which have are stored in
I'm trying to create a class with a built-in condition for one of its
I have a Backbone.js Collection and I have an array of model IDs that
I'm starting to learn backbone.js and I've built my first page and I want

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.