- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在这段涉及构造函数的 Google Closure javascript 代码片段中,为什么 goog.base(this);
是必需的? Foo
不是已经通过 goog.inherits(foo, goog.Disposable);
从 Disposable 继承了吗?
goog.provide('Foo');
/**
* @constructor
* @extends {goog.Disposable}
*/
Foo = function() {
goog.base(this);
}
goog.inherits(foo, goog.Disposable);
foo.prototype.doSomething = function(){
...
}
foo.prototype.disposeInternal = function(){
...
}
最佳答案
goog.inherits()
从子构造函数建立原型(prototype)链给父构造函数。
/**
* Inherit the prototype methods from one constructor into another.
* @param {Function} childCtor Child class.
* @param {Function} parentCtor Parent class.
*/
goog.inherits = function(childCtor, parentCtor) {
/** @constructor */
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
/** @override */
childCtor.prototype.constructor = childCtor;
};
除了原型(prototype)属性,构造函数可能有“自己的”属性(即添加到 this
的特定于实例的属性)。自 goog.inherits()
不调用父构造函数,自己的属性不复制到子构造函数和父级中的任何初始化代码都没有得到执行。由于这些原因,标准模式是 chain constructors如以下示例所示。
/**
* @param {string} name The parent's name.
* @constructor
*/
var Parent = function(name) {
/**
* @type {string}
* @private
*/
this.name_ = name;
}
/**
* @param {string} name The child's name.
* @constructor
* @extends {Parent}
*/
var Child = function(name) {
Parent.call(this, name);
}
goog.inherits(Child, Parent);
goog.base()
是一个调用父方法的辅助函数,这样你就可以不需要显式使用 call()或 apply() .
If [goog.base()] is called from a constructor, then this calls the superclass contructor with arguments 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. This calls the superclass' method with arguments 2-N.
This function only works if you use
goog.inherits
to express inheritance relationships between your classes.
在 Closure 代码中,通常使用 goog.base()
链接构造函数,而不是而不是显式调用父构造函数。
/**
* @param {string} name The child's name.
* @constructor
* @extends {Parent}
*/
var Child = function(name) {
goog.base(this, name);
}
goog.inherits(Child, Parent);
goog.inherits()
的模式的详分割析,请参阅第 6 章:“代码重用模式” - Classical Pattern #5--A Temporary Constructor) 关于javascript - 为什么除了 `goog.base(this)` 之外还需要 `goog.inherits()` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11122608/
当我将 goog.require 放入 HTML 文件时.. base.js文件一定要写这样的script标签 然而,当我把一个由 depswriter.py 生成的依赖文件..它不会放置其他文件所
我正在学习 google 闭包,当我尝试使用 goog.ui 组件(其中的任何类)时,出现错误 goog.require 找不到:goog。用户界面。 我的代码是: goog.require('goo
在这段涉及构造函数的 Google Closure javascript 代码片段中,为什么 goog.base(this); 是必需的? Foo 不是已经通过 goog.inherits(foo,
谁能告诉我其中的区别。我刚刚开始关闭谷歌。 最佳答案 goog.provide 创建一个命名空间; goog.require 从给定 namespace “导入”代码,以便闭包编译器可以找到它。 通常
goog.structs 提供了什么而 goog.object 没有提供?我浏览了文档,但没有发现任何令人满意的东西,除了 goog.structs 的第一行说: Generics method fo
更新到 Google Closure Library 的修订版 2156 后,goog.require() 无法找到 goog.ui.AutoComplete。 svn update At revis
我有一个监听 DOM 元素上的事件的 View 类。它还会触发 Controller 监听的事件。 如何才能最好地与 Google Closure 的库配合使用? 这是我最好的镜头。 /** * @
我们正在尝试将项目的打包方式从 dojo 切换到 google 闭包,但到目前为止我们还没有成功。这是一个简单的例子,说明了我们正在努力完成的事情:
我对调用以下代码时发生的情况感到有点困惑: goog.net.XhrIo.send("/welcome", goog.bind(this.handleWelcome, this)); 我有一个带有这个
我正在尝试使用闭包编译器和闭包库。 当我使用该库时,一切正常,我在模拟中包含“base.js”,它适用于我的所有 javascript 文件。 当我“编译”我的应用程序时,问题就出现了:在输出文件中,
我想将 WebGL 与 Google Closure Compiler 结合使用(主要用于三维 View )。我查看了库源代码,但发现 goog.webgl 命名空间中当前只有一个文件,它只做常量值定
来自 Google 的 Closure 库: goog.inherits = function(childCtor, parentCtor) { /** @constructor */ fun
我到处搜索,似乎找不到这个问题的答案:Google 闭包编译器使用 goog.require 函数解析依赖项的顺序是什么。 示例: goog.require('work.Utils'); goog.r
当某些实验标志打开时,我将注入(inject)注册为: 'abcFactory': name.space.createSomeModel 并注入(inject)工厂如下: /** * @param
我正在尝试使用 Google 的 libphonenumber我项目中的库。我正在尝试使用 javascript 版本验证电话号码输入,但它给我一个“goog 未定义”错误。我是 javascript
我想用goog.addSingletonGetter() ( http://closure-library.googlecode.com/svn/!svn/bc/4/trunk/closure/goo
我做了一个 goog.Timer对象 ( http://closure-library.googlecode.com/svn/docs/class_goog_Timer.html ) 与 new go
我使用 Closure Compiler用于在高级模式下测试我的 js 代码的在线工具。 我的代码片段如下: // ==ClosureCompiler== // @compilation_level
我想用google闭包库的goog.module实现JS模块懒加载。 我已经能够通过键入 goog.require(module_name) 来使用这些模块。但是,脚本模块会在开始时以这种方式加载。所
我在我的项目中使用 Google 闭包库和编译器。 下一个字符串出现奇怪的错误: if (!properties.name) { throw {message: 'Widget name ex
我是一名优秀的程序员,十分优秀!