JavaScript Backbone View inheritance sharing properties backbone view inheritance

I am extending Backbone views like:

var baseView = Backbone.View.extend({
    fruits : [],
    listFruits : function() {
        console.log(this.fruits);
    }
});

var firstView = baseView.extend({
    initialize : function() {
        this.fruits.push("apple");
    }
});

var secondView = baseView.extend({
    initialize : function() {
        this.fruits.push("pear");
    }
});


var firstViewInstance = new firstView(); // ["apple", "pear"]
var secondViewInstance = new secondView(); // ["apple", "pear"]

firstViewInstance.listFruits();
secondViewInstance.listFruits();

To my mind, firstViewInstance and secondViewInstance should be entirely separate, but they are clearly related. How can I achieve two separate view objects, both inheriting from a common base, but not sharing data?

EDIT: Based on answers below I have an updated fiddle at http://jsfiddle.net/qZ7SU/ which seems to solve this problem. I'm still not entirely clear what is going on, but I think explicitly attaching fruits[] to this, creates it as an instance variable, rather than attaching it to the prototype.

Answer:1

This is because the reference to the original fruits-property is the one being copied to the children view. See underscore.js extend

_.extend = function(obj) {
  each(slice.call(arguments, 1), function(source) {
    if (source) {
      for (var prop in source) {
        obj[prop] = source[prop];
      }
    }
  });
  return obj;
};

To inherit from the common base, but not share data, you need to initialize the not-shared properties in the initialize-function.

var baseView = Backbone.View.extend({
  initialize: function() {
    this._initializeBase();
  },

  _initializeBase: function() {
    this.fruits = [];
  },

  listFruits : function() {
    console.log(this.fruits);
  }
});

var firstView = baseView.extend({
  initialize : function() {
    this._initializeBase();
    this.fruits.push("apple");
  }
});

This way the property will be added to the object when it is initialized and the data will not be shared.

Hope this helps!

Answer:2

I have a controller, simplified for brevity: .controller('myCtrl', ['$scope', 'data', function ($scope, data) { $scope.myVar = testFunction(data.name); function testFunction (name) { ... }; ...

I have a controller, simplified for brevity: .controller('myCtrl', ['$scope', 'data', function ($scope, data) { $scope.myVar = testFunction(data.name); function testFunction (name) { ... }; ...

  1. jasmine test scope variable
  2. angularjs test scope variable
  3. test-path variable scope
  4. junit test variable scope
  5. java test variable scope
  6. javascript test variable scope

I am trying to replace all the content, depending on what it is, with a corresponding unicode character in all anchors that are descendants of the class nav ( .nav a ). But this javascript / jQuery ...

I am trying to replace all the content, depending on what it is, with a corresponding unicode character in all anchors that are descendants of the class nav ( .nav a ). But this javascript / jQuery ...

Can anyone please help me how to do this? I would like to pass the message from the websocket to the serial port every time I receive a message var firmata = require('firmata'); var board = new ...

Can anyone please help me how to do this? I would like to pass the message from the websocket to the serial port every time I receive a message var firmata = require('firmata'); var board = new ...

  1. javascript message passing
  2. javascript message passing between windows

Is it possible to include JQuery in the here-doc section of perl. I tried but no success. Here is my code. my $cgi = CGI->new();print header();print start_html("JQuery in perl"); print <<JS;...

Is it possible to include JQuery in the here-doc section of perl. I tried but no success. Here is my code. my $cgi = CGI->new();print header();print start_html("JQuery in perl"); print <<JS;...