课程表

CoffeeScript 语法

CoffeeScript 类和对象

CoffeeScript 字符串

CoffeeScript 数组

CoffeeScript 日期和时间

CoffeeScript 数学

CoffeeScript 方法

CoffeeScript 元编程

CoffeeScript jQuery

CoffeeScript 正则表达式

CoffeeScript 网络

CoffeeScript 设计模式

CoffeeScript 数据库

CoffeeScript 测试

工具箱
速查手册

class/继承/super

当前位置:免费教程 » JS/JS库/框架 » CoffeeScript

JavaScript 的原型继承有点烧脑, 存在大量的类库用于在 JavaScript 的原型之上实现更清晰的 class 继承, 比如: Base2, Prototype.js, JS.Class. 这些类库提供了语法糖, 但如果不是因为一些例外的话原生的继承完全是可用的, 例外比如: 很难调用 super(当前函数的原型上的实现), 很难正确设置原型链.

相比重复地设置函数的原型, CoffeeScript 提供了一个基础的 class 结构, 你可以在一个定义的表达式里完成命名 class, 定义父类, 赋值原型上的属性, 定义构造器.

构造函数被命名, 这对查看调用栈有更好的支持. 下面例子中的第一个类, this.constructor.name is "Animal".

CoffeeScript:
  1. class Animal
  2. constructor: (@name) ->
  3.  
  4. move: (meters) ->
  5. alert @name + " moved #{meters}m."
  6.  
  7. class Snake extends Animal
  8. move: ->
  9. alert "Slithering..."
  10. super 5
  11.  
  12. class Horse extends Animal
  13. move: ->
  14. alert "Galloping..."
  15. super 45
  16.  
  17. sam = new Snake "Sammy the Python"
  18. tom = new Horse "Tommy the Palomino"
  19.  
  20. sam.move()
  21. tom.move()
编译成JS:
  1. var Animal, Horse, Snake, sam, tom,
  2. __hasProp = {}.hasOwnProperty,
  3. __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; };
  4.  
  5. Animal = (function() {
  6. function Animal(name) {
  7. this.name = name;
  8. }
  9.  
  10. Animal.prototype.move = function(meters) {
  11. return alert(this.name + (" moved " + meters + "m."));
  12. };
  13.  
  14. return Animal;
  15.  
  16. })();
  17.  
  18. Snake = (function(_super) {
  19. __extends(Snake, _super);
  20.  
  21. function Snake() {
  22. return Snake.__super__.constructor.apply(this, arguments);
  23. }
  24.  
  25. Snake.prototype.move = function() {
  26. alert("Slithering...");
  27. return Snake.__super__.move.call(this, 5);
  28. };
  29.  
  30. return Snake;
  31.  
  32. })(Animal);
  33.  
  34. Horse = (function(_super) {
  35. __extends(Horse, _super);
  36.  
  37. function Horse() {
  38. return Horse.__super__.constructor.apply(this, arguments);
  39. }
  40.  
  41. Horse.prototype.move = function() {
  42. alert("Galloping...");
  43. return Horse.__super__.move.call(this, 45);
  44. };
  45.  
  46. return Horse;
  47.  
  48. })(Animal);
  49.  
  50. sam = new Snake("Sammy the Python");
  51.  
  52. tom = new Horse("Tommy the Palomino");
  53.  
  54. sam.move();
  55.  
  56. tom.move();

如果你不喜欢用 class 的裁判法定义原型, CoffeeScript 提供了一些低级的方便写法. extends 操作符可以用来恰当地定义任何一对构造函数的原型链; 用 :: 可以快速访问对象的原型; super() 可以编译为一个父类上同名方法的调用.

CoffeeScript:
  1. String::dasherize = ->
  2. this.replace /_/g, "-"
编译成JS:
  1. String.prototype.dasherize = function() {
  2. return this.replace(/_/g, "-");
  3. };

最后, class 定义是可执行的代码, 这样就可能进行元编程. 因为在 class 定义的上下文当中, this 是类对象本身(构造函数), 可以用 @property: value 赋值静态的属性, 也可以调用父类的方法: @attr 'title', type: 'text'.

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号