Why is the __extend () method used?

I am curious to create an MVC javascript framework for fun and as a learning experience.

Inside backbone.js https://github.com/jashkenas/backbone/blob/master/backbone.js, the author uses the underscore method _.extend()to "expand" the base object.

Starting with Model, I'm trying to figure it out.

// Attach all inheritable methods to the Model prototype.
_.extend(Model.prototype, Events, {});

How it works? Modelalready defined before, then the author wants Copy all of the properties in the source objects over to the destination object http://underscorejs.org/#extend , does he create a whole bunch of methods inside to interact with the original model as a prototype?

Why not do

Backbone.Model.prototype.get = function() {
    return 'Do Get';
}

Backbone.Model.prototype.set = function() {
    return 'Do Set';
}

Or is it even better to use Es6? Is the following even the same as the one above?

class Model extends Backbone {
  get() {
    return 'Do Get';
  }
  set() {
    return 'Do Set';
  }
}

, - ? , , es6 -, , .

- .

+4
1

_. extend - .

, _.extend(obj1, obj2, ..., objN) , .

_.extend(Backbone.Model.prototype, {
  get: function() {
    return 'Do Get';
  }
}

// Is an equivalent of
Backbone.Model.prototype.get = function() {
    return 'Do Get';
}

Backbone.Events

- , , .

, . Backbone.Model.prototype JS - Prototype?

ES6, , class .

// This will won't work, since you can't extend objects. It has to be a constructor.
class Model extends Backbone {
  ...
}

, Backbone ES6, . Backbone.js ES6 .

+4

Source: https://habr.com/ru/post/1624894/


All Articles