简体   繁体   English

使用Google Closure继承

[英]Inheritance using Google Closure

I have a base class named 'baseScreen' as follows: 我有一个名为“ baseScreen”的基类,如下所示:

digient.casino.ui.baseScreen = function() {
    goog.debug.Logger.getLogger('demo').info('baseScreen');
    this.background                             =   goog.dom.createDom('div', {'class': 'backgroundHolder'}, '');
    console.log(this);
}

digient.casino.ui.baseScreen.background         =   null;

digient.casino.ui.baseScreen.prototype.load     =   function() {
    var self                                    =   this;
    goog.debug.Logger.getLogger('demo').info('baseScreen : load');
    goog.dom.appendChild(document.body, this.background);
    <!-- screen setup code goes here -->
};

digient.casino.ui.baseScreen.prototype.resize  =   function(newSize) {
    goog.debug.Logger.getLogger('demo').info('baseScreen : resize');
};

in onLoad , if I load baseScreen as onLoad ,如果我将baseScreen加载为

var sc = new digient.casino.ui.baseScreen();
sc.load();

its working fine. 它的工作正常。

Then I derive a screen named registerScreen as follows: 然后,我得到一个名为registerScreen的屏幕,如下所示:

digient.casino.ui.registerScreen = function() {                                                     
    goog.debug.Logger.getLogger('demo').info('registerScreen');                                     
    digient.casino.ui.baseScreen.call();
};                                                                                                  
goog.inherits(digient.casino.ui.registerScreen, digient.casino.ui.baseScreen); 

When I try to load object of registerScreen , its throwing an error on the line where I try to append this.background to document.body and weirdly console.log(this) in line 4 prints window object instead of baseScreen or registerScreen object. 当我尝试加载registerScreen对象时,它在我尝试将this.background附加到document.body并在第4行中奇怪地console.log(this)的行上引发了错误,它打印window对象而不是baseScreenregisterScreen对象。

Whats wrong with my code? 我的代码有什么问题? Do I need to override load, resize in my derived class? 我是否需要重写派生类中的负载并调整其大小? (tried this, but failure) or any other problem? (尝试过此方法,但失败了)或其他任何问题?

Alternatively, you could also replace the call mentioned by @felix-kling with: 另外,您也可以将@ felix-kling提到的调用替换为:

goog.base(this);

which does the exact same thing. 确实做同样的事情。

One note about goog.base, from the docs: 来自文档的有关goog.base的注释:

If this is called from a constructor, then this calls the superclass constructor with arguments 1-N. 如果从构造函数调用此函数,则将使用参数1-N调用超类构造函数。 If this is called from a prototype method, then you must pass the name of the method as the second argument to this function. 如果从原型方法中调用此方法,则必须将方法名称作为第二个参数传递给此函数。 If you do not, you will get a runtime error. 否则,您将收到运行时错误。

see also: 也可以看看:

You have to call baseScreen will the current registerScreen instance: 您必须调用baseScreen将当前的registerScreen实例:

digient.casino.ui.baseScreen.call(this);

Otherwise, your function call is equivalent to digient.casino.ui.baseScreen() , hence this refers to the global object window . 否则,您的函数调用等效于digient.casino.ui.baseScreen() ,因此this是指全局对象window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM