如何创建带有下划线自己可继承类 [英] How to create own inheritable class with underscore

查看:197
本文介绍了如何创建带有下划线自己可继承类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立像对象/类Backbone.Model。

I want to built a Backbone.Model like object/class.

我通过骨干文档看了一下,发现他们创造一个可继承对象/类及其继承属性,分为两个步骤的方法:

I looked through the Backbone docs and found that they create a inheritable object/class with its inheritable attributes, methods in two steps:

我。与某些属性创建一个函数

I. Create a function with some attributes

var Model = Backbone.Model = function(attributes, options) {
    var defaults;
    attributes || (attributes = {}); //?
    if (options && options.parse) attributes = this.parse(attributes); //?
    if (defaults = getValue(this, 'defaults')) {
      attributes = _.extend({}, defaults, attributes); // ?
    }
    if (options && options.collection) this.collection = options.collection;
    this.attributes = {};
    this._escapedAttributes = {};
    this.cid = _.uniqueId('c');
    this.changed = {};
    this._silent = {};
    this._pending = {};
    this.set(attributes, {silent: true});

    this.changed = {};
    this._silent = {};
    this._pending = {};
    this._previousAttributes = _.clone(this.attributes);
    this.initialize.apply(this, arguments);
};

二。使用下划线的扩大给它一定的功能

II. use underscore's extend to give it some functions

_.extend(Model.prototype, Events, { // Events?
   changed: null,

   _silent: null,

   _pending: null,

   idAttribute: 'id',

   initialize: function(){},

   toJSON: function(options) {
       return _.clone(this.attributes);
   }

   // other Model methods...
};

我对这种行为的一些问题:

I have some questions about this behaviour:


  1. 这是什么线3-4在第一部分做

  2. 在第6行,会发生什么?

  3. 为什么我们overgive_.extend的活动对象,我还有什么可给的参数?

还有什么我必须支付约注意力?

Is there anything else I have to pay attention about?

问候

推荐答案

3号线,属性|| (属性= {}); 是<一个例子href=\"https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators#Short-Circuit_Evaluation\"相对=nofollow>短路评价。
如果属性有falsy valye,那么JavaScript的评估或前pression的第二部分。这部分受让人属性 的值{} ,一个空的对象。最终的结果是,如果属性然后,分配属性空对象null或undefined(典型案例)。

Line 3, attributes || (attributes = {}); is an example of short circuit evaluation. If attributes has a falsy valye, then javascript will evaluate the second part of the OR expression. That part assigns attributes a value of {}, an empty object. The end result is, if attributes is null or undefined (typical case) then, assign attributes an empty object.

这行相当于说:

if (attributes == false) { // attributes can be any value that evaluates to false
                           // attributes could be null, undefined, false, 0 etc
   attributes = {};
}

与4号线相同的情况下,如果(选项和放大器;&安培; options.parse)属性= this.parse(属性);

如果一个选项对象存在,即它不是null或undefined,而选项对象有一个有效的属性名为解析,然后分配属性值 this.parse(属性);

If an options object exists, ie its not null or undefined, and the options object has a valid property called parse, then assign attributes the value of this.parse(attributes);

编辑1:

_。延长线可以由下划线解释。 JS文件

它是一个标准的模式来创建一个新的对象将包含所有对象通过属性。在第一个例子属性= _.extend({},默认值,属性); // <?/ code>图案用于覆盖用什么都可能已经传递的默认值。你会看到这种模式被大多数的插件,让您在选择对象传递。如果你不传任何东西,将使用缺省值。如果只有几个属性通,其余的将从默认得到它们的值。

Its a standard pattern to create a new object that will contain properties of all passed in objects. In the first example attributes = _.extend({}, defaults, attributes); // ? the pattern is used to override the default values with what ever may have been passed. You would see this pattern followed in most plugins that allow you to pass in an options object. If you dont pass in anything, the default values will be used. If you pass in only a few properties, the remaining will get their values from the defaults.

http://api.jquery.com/jQuery.extend/ 是不是同样的事情。但它的非常相似。他们的文档要好得多。你会理解的概念更好。

http://api.jquery.com/jQuery.extend/ is NOT the exact same thing. But its very similar. Their documentation is much better. You will understand the concept better.

编辑2:

借助骨干活动类有一些事件处理相关的方法。骨干Model类也需要能够利用的事件处理。它需要引发事件告诉视图重新渲染本身,它需要当视图改变基础数据,以倾听来自DOM事件。此事件处理功能可以或者已经从头定义模型类时重新写入,或示范类可以被扩展以获得从事件类这些方法。这后者是发生了什么。

The Backbone Events class has some event handling related methods. The Backbone Model class also needs to be able to make use of event handling. It needs to raise events to tell the view to re-render itself, it needs to listen to events from the dom when the view changes the underlying data. This event handling functionality could either have been re-written from scratch when defining the Model class, or the Model class could be extended to get these methods from the Events class. That latter is what is happening.

如果你读了$ .extend或_.extend的文档,你会发现Model类被扩展不仅仅是在事件的性质,也像其他的toJSON属性和初始化等等...

If you read the documentation for the $.extend or _.extend, you will realize that the Model class is being extended not just with the properties under Events, but also with other properties like toJSON and initialize etc...

这篇关于如何创建带有下划线自己可继承类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆