Javascript 使用Google闭包库剖析正确的继承/子类实现

Javascript 使用Google闭包库剖析正确的继承/子类实现,javascript,inheritance,google-closure,limejs,Javascript,Inheritance,Google Closure,Limejs,我开始使用Google的闭包库(以及Lime.js),并尝试创建一个非常基本的场景,其中包含Lime.js的Lime.Layer类的自定义子类的一些实例 据我所知(正如我在各种示例和Google自己的文档中所读到的),您的子类中需要有7项才能正确地从其预期的超类继承,并可用于其他类: 提供(“子类名称”) goog.require(“超类的名称”) 在构造函数@constructor上面的注释中 在上面的注释中,构造函数@extends name.of.superclass 在构造函数内部,调

我开始使用Google的闭包库(以及Lime.js),并尝试创建一个非常基本的场景,其中包含Lime.js的Lime.Layer类的自定义子类的一些实例

据我所知(正如我在各种示例和Google自己的文档中所读到的),您的子类中需要有7项才能正确地从其预期的超类继承,并可用于其他类:

  • 提供(“子类名称”)
  • goog.require(“超类的名称”)
  • 在构造函数@constructor上面的注释中
  • 在上面的注释中,构造函数@extends name.of.superclass
  • 在构造函数内部,调用name.of.superclass.call(this)
  • 在构造函数之后,goog.inherits(name.of.subclass,name.of.superclass)
  • 最后,使用goog.exportSymbol('name.of.subclass',name.of.subclass')导出构造函数
我的项目结构很简单。“index.html”与一个名为“js”的文件夹位于主层,其中包含我所有的自定义JavaScript文件。我已经运行了其他更简单的测试,确认Closure和Lime都正确加载和初始化。我只是想在更多OO风格的模式中迈出下一步。下面是我的代码和遇到的错误


index.html

<!DOCTYPE html>
<html>
    <head>
        <title>LimeTest</title>
        <script type="text/javascript" src="../closure/closure/goog/base.js"></script>
        <script type="text/javascript" src="js/limetest.js"></script>
    </head?
    <body onload="limetest.start()"></body>
</html>

js/thing.js

goog.provide('stupid.thing');

goog.require('lime');
goog.require('lime.Layer');

/*
*   @constructor
*   @extends lime.Layer
 */
stupid.thing = function ()
{
    lime.Layer.call(this);
};
goog.inherits(stupid.thing, lime.Layer);

goog.export('stupid.thing', stupid.thing);

我在Chrome中遇到的错误有:

base.js:     634   goog.require could not find: stupid.thing
base.js:     634   goog.logToConsole_
base.js:     675   goog.require
limetest.js: 10    (anonymous function)

base.js:     677   Uncaught Error: goog.require could not find: stupid.thing
base.js:     677   goog.require
limetest.js: 10    (anonymous function)


我缺少什么?

基本上,解决依赖关系有两种选择: 1) 手动加载依赖脚本 2) 加载一个deps.js文件,该文件提供依赖项的路径

通常情况下,会生成“deps.js”(我不太熟悉,lime是否为您这样做)


作为补充说明,您可能想尝试使用goog.defineClass,它从类定义中删除了一些样板文件(@constructor、@extends和goog.inherits是隐含的)。

这一切是为了什么?我不知道它应该做什么…它应该不会返回错误。显然我有更大的意图,这只是一个例子。从来没有人问过“foo和bar有什么意义?”@HorsePickle:这些错误似乎与继承实现没有任何关系,不是吗?请创建一个产生这些错误的单词,并在其后添加您的问题。
base.js:     634   goog.require could not find: stupid.thing
base.js:     634   goog.logToConsole_
base.js:     675   goog.require
limetest.js: 10    (anonymous function)

base.js:     677   Uncaught Error: goog.require could not find: stupid.thing
base.js:     677   goog.require
limetest.js: 10    (anonymous function)