在主干0.5.3源代码中的属性vs this.attributes

时间:2017-03-25 15:44:47

标签: javascript backbone.js

我正在学习tutorial on backbone source codes。我对以下代码中attributesthis.attributes之间的区别感到困惑。有人可以解释一下吗?感谢

我知道当前版本是1.3.3,但我只是对旧源代码中的语法感到好奇,以及它做了什么。

Backbone.Model = function(attributes, options) {
  var defaults;
  attributes || (attributes = {});
  if (defaults = this.defaults) {
    if (_.isFunction(defaults)) defaults = defaults.call(this);
    attributes = _.extend({}, defaults, attributes);
  }
  this.attributes = {};
  this._escapedAttributes = {};
  this.cid = _.uniqueId('c');
  this.set(attributes, {silent : true});
  this._changed = false;
  this._previousAttributes = _.clone(this.attributes);
  if (options && options.collection) this.collection = options.collection;
  this.initialize(attributes, options);
};

1 个答案:

答案 0 :(得分:0)

  • this.attributes是Backbone模型的属性。它是保存模型数据的哈希值。在构造函数中,它被初始化为空对象。您可以从课程modelInstance.attributes外部访问它。

  • attributes 只是参数的名称,其中初始数据已传递给模型。它可以是任何名称,因为它是构造函数中的局部变量。它无法从构造函数外部访问。

创建新模型实例时,可以传递要立即设置的属性。

var data = {
    initial: "data",
    id: "1234"
    // etc.
};

// here, data is passed into the `attributes` parameter.
var modelInstance = new Backbone.Model(data);

我将本地attributes变量的名称更改为initialData以说明该概念。我还简化了一些构造函数,以更好地显示正在进行的操作。

Backbone.Model = function(initialData, options) {
    // Initializes properties
    this.attributes = {};
    this._escapedAttributes = {};
    this.cid = _.uniqueId('c');

    // Makes sure that the initialData variable is an object.
    if (!initialData) { // if it's a falsy value (undefined, 0, null, "")
        initialData = {}; // assign it a new object
    }

    // if the `defaults` property exists on this model class
    if (this.defaults) {
        var defaults;

        // and if it's a function
        if (_.isFunction(defaults)) {
            // call it and save the return value
            defaults = this.defaults();
        }

        // merge the defaults with the initial data, where any attribute present
        // in the initialData object overwrites the default value.
        initialData = _.extend({}, defaults, initialData);
    }

    // Apply the initialData to the `this.attributes` with all of other things
    // `set` does. No events are triggered since `silent` is set to true.
    this.set(initialData, { silent: true });

    // cancel the change flag since it's the initialization of a new instance.
    // Even if it changed from an empty object to the initialData value, it 
    // doesn't make sense to flag it as changed.
    this._changed = false;

    // a shallow copy of the attributes to compare to when changes are made.
    this._previousAttributes = _.clone(this.attributes);

    // A model keeps a reference to the collection instance in which it was 
    // created (when passing raw data to a collection) to use its `url` property
    // when none are set in the model class.
    if (options && options.collection) this.collection = options.collection;

    // calls the initialize function, which is empty by default and is just 
    // a convinience for the developer to override.
    this.initialize(initialData, options);
};