如何理解由 CoffeeScript 的 'extends' 关键字生成的 JavaScript 代码
How to understand the JavaScript code generated by CoffeeScript's `extends` keyword
这是由CoffeeScript的extends
关键字生成的JavaScript代码。原型链是如何设置的?
var __hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
var __hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) {
// Copy "static" attributes from the parent constructor to the child constructor
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
// This is the surrogate constructor, used so you don't need
// to instantiate an instance of the parent just to setup the prototype chain
// the statement in the surrogate constructor properly attaches
// the constructor property to object
function ctor() { this.constructor = child; }
// Attach the parent's prototype to the surrogate constructor
ctor.prototype = parent.prototype;
// This is setting up the chain, attaching an instance of a constructor whose
// prototype is set to the parent to the prototype property of the child
// In naive implementations, this would be child.prototype = new parent();
child.prototype = new ctor;
// Allows access to the parent from user code, and used by the `super` keyword
child.__super__ = parent.prototype;
return child;
};
请参阅 http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html(我自己的博客文章)
相关文章:
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 面向对象的Javascript代码在IE7中不起作用
- 通过命令行/批处理文件打开页面时,将javascript代码注入Google Chrome
- JavaScript代码问题:我正在将对象转换为数组
- Javascript阻止其他Javascript代码
- JavaScript代码未正确检查ajax请求
- 如何调试Javascript代码或函数
- 为什么我在这个javaScript代码中使用NaN
- 将javascript代码转换为jquery代码时出错
- 如何从Objective-C代码中调用javascript代码
- 有什么工具可以轻松读取javascript代码吗
- 这个javascript代码是如何编写的
- 如何解密此javascript代码
- SIMPLE Javascript代码,用于显示谷歌电子表格中单个字段的数据
- HTML标记,包含带引号的JavaScript代码中的引号
- 如何在Win8Metro应用程序的Javascript代码中捕获自己的C#事件
- addEventListener的Javascript代码不工作!?(单击时打开放大的img)
- JavaScript代码无法在表单上呈现部分
- 以下 JavaScript 代码与 Facebook 相关
- 使用解析为javascript源的.php,如何使用条件语句将javascript代码封装在php括号之间