this.attributes
バックボーンモデルのプロパティです。これはモデルのデータが保持されるハッシュです。コンストラクタでは、空のオブジェクトとして初期化されます。クラスmodelInstance.attributes
の外からアクセスできます。
attributes
は、初期データがモデルに渡されたパラメータの名前だけです。コンストラクタ関数内のローカル変数であるため、任意の名前を使用できます。コンストラクタ関数の外部からアクセスすることはできません。
新しいモデルインスタンスを作成するときに、すぐに設定する属性を渡すことができます。
var data = {
initial: "data",
id: "1234"
// etc.
};
// here, data is passed into the `attributes` parameter.
var modelInstance = new Backbone.Model(data);
私は概念を説明するためにinitialData
にローカルattributes
変数の名前を変更しました。私はまた、何が起こっているかをより良く示すために、コンストラクタ関数を単純化しました。
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);
};
私はあなたが何を説明したいのか分かりません。 JavaScriptで「これ」が何を意味するのか知っていますか? – Touffy
'' 'this.attributes''はここ(' '' this.attributes = {}; this._previousAttributes = _.clone(this.attributes); '' ')のためのものではありません。 '' 'attributes'''はコンストラクタ関数のいくつかのフィールドを設定するために使われます。 – BAE
Backbone Modelsの 'attributes'プロパティは、モデルのフィールドを内部的に保存するために使用されます。モデルの 'get'、' set'、 'unset'メソッドで通常操作するものです。 – Touffy