Javascript 私有函数类似于使用文本的Java

Javascript 私有函数类似于使用文本的Java,javascript,html,cocos2d-x,cocos2d-js,Javascript,Html,Cocos2d X,Cocos2d Js,我的对象设置如下所示: var StartScreenLayer = cc.Layer.extend({ ctor: function () { this._super(); // this function can call createBackground! this.createBackground(); }, callCreateBackgroundToo: function () { // I can call createBackground too!

我的对象设置如下所示:

var StartScreenLayer = cc.Layer.extend({
ctor: function () {
    this._super();
    // this function can call createBackground!
    this.createBackground();
},
callCreateBackgroundToo: function () {
   // I can call createBackground too!
   this.createBackground();
},
createBackground: function () {
});

我如何安排它,使createBackground是私有的,但其他对象不能调用类似于
screenLayer.createBackground()
的东西,并在对象上抛出
createBackground is undefined
类型错误?

显然,惯例是在它前面加一个下划线。但这只是一个惯例,因此您仍然可以调用“private”函数

我正在使用的cocos2d库就是这样做的,所以我想我也会这样做

编辑:

根据沃伦的建议,我采用了以下方法:

var StartScreenLayer = cc.Layer.extend(function() {
  function callCreateBackgroundToo() {
     // I can call createBackground too!
     createBackground(this);
  }
  function createBackground() {
  }
  return {
    ctor: function () {
        this._super();
        // this function can call createBackground!
        createBackground.call(this);
    }
  }
}());

看一看。你可能想使用
createBackground.call(this)
来获得一些答案。嘿,我看了一下,看起来你必须跳出一堆障碍才能让它工作。我刚刚决定使用一个现有的约定,即在函数前面加下划线。加下划线确实也是一个好的、可访问的解决方案。仅供参考其他解决方案(但可能不是那么容易获得)哦,哇,我喜欢你的示例2!加上这个作为答案,我明天就接受。