私人和公共变量骨干视图 [英] Private and public variables to a backbone view

查看:88
本文介绍了私人和公共变量骨干视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里会你把你的私有变量和你的公共骨干视图。

In a backbone view where would you put your private variables and your public.

现在,我有这样的事情:

Right now I have something like this:

myView = Backbone.View.extend({

  initialize: function(options){
    this.myPublic = "I'm public";
  }

});

我试过初始化方法之前添加 VAR myPrivate ,但它抛出一个错误。那些只在视图中使用私有变量会去哪里?

I tried adding a var myPrivate before the initialize method but it threw an error. Where would private variables that are only used within the view go?

推荐答案

我建议你使用初始化方法周围所有其他方法关闭。我觉得这会给你的行为与我们在经典的继承语言,如C ++和Java获得更一致的:

I suggest you use the initialize method as a closure around all other methods. I think this will give you behaviour more consistent with what we get in classical inheritance languages like C++ and Java:



myView = Backbone.View.extend({

  initialize: function(options){
    var myPrivate = "I'm private";

    this.myPublic = "I'm public";

    this.getPrivate = function () {
      return myPrivate;
    };

    this.setPrivate = function (value) {
        if (typeof(value) === 'string') {
            myPrivate = value;
            return true;
        } else {
            return false;
        }
    };
  }
});

这篇关于私人和公共变量骨干视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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