【问题标题】:Private-like properties in models or views of Backbone.jsBackbone.js 的模型或视图中的私有属性
【发布时间】:2012-02-14 01:08:29
【问题描述】:

模型中是否可以有私有属性?就像(构造函数)函数中本地声明的变量一样,不附加到this,而是在本地声明并且仅由(构造函数)函数中定义的任何内容可见。 没有 BB 视图的示例:

function MyView(aModel){

  var $internalInput = $('<input>');

  this.render: function($where){
     $internalInput.val(aModel.get('SomeProperty'));
     $where.append($('<div class="inputWraper">').append($internalInput));
  };
  this.toggleReadonly: function() {
    toggle $internalInputs readonly attribute
  }
  ...
  + Code to bind input.val to some aModel property(ies) and setup events
  ...
}

请注意,internalInput 无法被外界访问,aModel 也无法访问(至少通过 MyView)。 所以如果我想用Backbone.View来实现上面的MyView,我该怎么做并保持$internalInput'private'?

【问题讨论】:

  • 请注意,“私有”的唯一作用就是让您的代码变慢。
  • 你能详细说明一下吗?我只是想要它用于信息隐藏目的,以便我将来可以更改它,而不用担心有人可能会因为直接使用它而依赖它。但是,如果这意味着减慢代码速度,那么这就是一个问题。我在哪里可以找到有关您的索赔的更多信息?
  • Closures have massive overhead, Private state is expensive but can be done better。我个人的建议是使用_foo 来处理可能发生变化的internal 属性。此外,如果有人依赖 internal 属性,那么当您的 API 更改时,这是 他们的 问题,而不是您的问题

标签: javascript backbone.js private


【解决方案1】:

在定义 Backbone 对象时,您应该能够通过将 IIFE 传递给 extend 来获得私有数据,而不仅仅是一个普通对象。例如:

var Thing = Backbone.Model.extend((function () {
  var foo = "Private data!";

  return {
    bar: function () {
      console.log(foo);
    }
  };
})());

【讨论】:

  • 这会给你一些更类似于私有静态属性的东西。如果您希望模拟私有实例属性(例如 OP 的 var-in-constructor 技术),您将被共享状态所抛弃。
  • 毫无疑问它会起作用,但在主干中实现私有变量是唯一/更好的方法吗?这个答案说出了不同的方法stackoverflow.com/a/16320901/2253756
【解决方案2】:

你最好和

var Thing = Backbone.Model.extend(
    {
        constructor : function ()
        {
            var _value = "Private data!";

            this.getValue = function ()
            {
                return _value;
            };
            this.setValue = function (value)
            {
                _value = value;
            };
        }
    });

【讨论】:

【解决方案3】:

Javascript 很有趣!

var Thing = (function () {
    var number_of_things = 0;

    return function (options) {
        var value = "Private data!";

        return new ( Backbone.Model.extend({
            constructor: function constructor () {
              number_of_things += 1;
            },

            getValue: function getValue () {
                return value;
            }
        }) )();
    };
}());

我有点担心这个“事物”的每个实例也是一个子类,用 OOP 术语来说。

【讨论】:

    【解决方案4】:

    在将 Broserify.js 与 Backbone 一起使用(以及实际上任何上述中等项目)的上下文中,我发现了以下拥有私有变量和函数的方法:

    myView.js

    'use strict';
    
    var config     = require('../config.js'),
    
        private_var   = 'private variable',
        my_private_fn = function() {
            ...
        };
    
    
    module.exports = Backbone.Model.extend({
        initialize: function() {
            this.my_public = 'public variable');
    
            console.log('This is my' + this.my_public);
            console.log('This is my' + my_private);
        },
    });
    

    这里的想法是使用 Browserify :P

    【讨论】:

      【解决方案5】:

      最简单的方法如下:

      ...
      initialize:function(properites){            
          // Init the logic with private and public methods/variable
          this.logic.initFirst(this);
          // Use public methods
          this.logic.doSomething();
      },  
      
      logic:{         
          initFirst:function(modelOrView){
              // Do not continue if already initiated
              if( this.instance !== undefined ) return;
      
              // Write all logic here
              this.instance = (function(logic, modelOrView){               
                  // Private variables
                  var private = "private";                
      
                  // Public methods
                  logic.doSomething = function(){
                      console.log(private, modelOrView);
                  };  
      
                  // Private methods
                  function hidden(){
      
                  }
      
              }(this, modelOrView));  
          }
      
      },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-09
        相关资源
        最近更新 更多