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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:08:24+00:00 2026-06-16T21:08:24+00:00

I am programming with Backbone.js in CoffeeScript and have a problem with inheritance in

  • 0

I am programming with Backbone.js in CoffeeScript and have a problem with inheritance in relation with lodash and the merge function.

There is a superclass

class NavigationView extends Backbone.View
  config:
    test:
      string: "Test"

and two classes derived from it

class SubView extends NavigationView

  initialize: ->
      # Setting the view's template property using the Underscore template method
    _.merge @config, {
        test:
          string: "bla"
      }

class IndexView extends NavigationView
...

If I change within SubView’s function initialize the config variable it is also changed in an instance of IndexView.

I instantiate my objects like so, within a BackBone.Router class:

index: () ->
    # Instantiates a new view which will render the header text to the page
    new IndexView()

  sub: () ->
    new SubView()

I created a fiddle to show it: http://jsfiddle.net/hijolan/9VeND/

Any ideas how to do that?

Best regards, hijolan

  • 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-16T21:08:25+00:00Added an answer on June 16, 2026 at 9:08 pm

    Your problem is that _.merge modifies its first argument in-place:

    _.merge(object [, source1, source2, …])

    Merges enumerable properties of the source object(s) into the destination object.

    Note that where the documentation says destination it really means object. The intent of merge is to be a deep version of _.extend and the Underscore docs are explicit about what happens:

    extend _.extend(destination, *sources)

    Copy all of the properties in the source objects over to the destination object, and return the destination object.

    You’ll notice that lodash’s extend (AKA assign) also gets the parameter names mixed up:

    _.assign(object [, source1, source2, …])

    Assigns own enumerable properties of source object(s) to the destination object.

    Again they mean object when they say destination.

    When you do this:

    class NavigationView extends Backbone.View
      config:
        test:
          string: "Test"
    

    The config ends up attached to the prototype for NavigationView and so the exact same config object will be seen by NavigationView and its subclasses. That means that @config is the prototype’s config in your initialize:

    _.merge @config, { test: { string1: "blub" } }
    

    so _.merge will merge the new values right into the prototype’s config and that makes the change visible all the way up to NavigationView and down into all of its subclasses. If you trace the inheritance back up, you’ll find that @config in that context is from NavigationView so your _.merge is just a tricky and confusing way of writing:

    _.merge NavigationView::config, ...
    

    The root of the problem is that _.extend and _.merge modify their first argument. The way out of this trap is to supply a destination object that is safely writeable:

    @config = _.merge { }, @config, { test: { string1: 'blub' } }
    # ----------------^^^
    

    Demo: http://jsfiddle.net/ambiguous/7j2FM/

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

Sidebar

Related Questions

(programming in iPhone objective-C) I have a class level NSString* that I create, add
Im using backbone, require.js and coffeescript in a application. Programming my app, discovered i
While programming for iOS I encountered the following scenario: I have a singleton class
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating
Android programming. I have a json returning the date from a database as a
Programming Environment: Visual Studio 2010 Programming Language: VB.NET I have a tabbed web-browser that
Im programming a little game where two Players have to click some Buttons it
Programming language: Java Task: designing a hash function that maps Chinese Strings to numbers
Some programming languages have the feature of adding Categories to classes (for example Objective-C),
What programming tips / tricks have you learned for styling views on the iPhone

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.