- CoffeeScript 教程
- CoffeeScript - 首页
- CoffeeScript - 概述
- CoffeeScript - 环境
- CoffeeScript - 命令行工具
- CoffeeScript - 语法
- CoffeeScript - 数据类型
- CoffeeScript - 变量
- CoffeeScript - 运算符和别名
- CoffeeScript - 条件语句
- CoffeeScript - 循环
- CoffeeScript - 列表推导式
- CoffeeScript - 函数
- CoffeeScript 面向对象
- CoffeeScript - 字符串
- CoffeeScript - 数组
- CoffeeScript - 对象
- CoffeeScript - 范围
- CoffeeScript - 展开运算符
- CoffeeScript - 日期
- CoffeeScript - 数学
- CoffeeScript - 异常处理
- CoffeeScript - 正则表达式
- CoffeeScript - 类与继承
- CoffeeScript 高级
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用资源
- CoffeeScript - 快速指南
- CoffeeScript - 有用资源
- CoffeeScript - 讨论
CoffeeScript - 类与继承
JavaScript 没有提供 class 关键字。我们可以使用对象及其原型在 JavaScript 中实现继承。每个对象都有自己的原型,它们从其原型继承函数和属性。由于原型也是一个对象,因此它也有自己的原型。
尽管原型继承比经典继承强大得多,但对于新手用户来说,它却难以理解且令人困惑。
CoffeeScript 中的类
为了解决这个问题,CoffeeScript 提供了一个称为 class 的基本结构,该结构是使用 JavaScript 的原型构建的。您可以使用 class 关键字在 CoffeeScript 中定义类,如下所示。
class Class_Name
示例
考虑以下示例,这里我们使用关键字 class 创建了一个名为 Student 的类。
class Student
如果您编译上述代码,它将生成以下 JavaScript 代码。
var Student;
Student = (function() {
function Student() {}
return Student;
})();
实例化类
我们可以像其他面向对象编程语言一样,使用 new 运算符实例化类,如下所示。
new Class_Name
您可以使用 new 运算符实例化上面创建的 (Student) 类,如下所示。
class Student new Student
如果您编译上述代码,它将生成以下 JavaScript 代码。
var Student;
Student = (function() {
function Student() {}
return Student;
})();
new Student;
定义构造函数
构造函数是在实例化类时调用的函数,其主要目的是初始化实例变量。在 CoffeeScript 中,您可以通过创建一个名为 constructor 的函数来定义构造函数,如下所示。
class Student constructor: (name)-> @name = name
在这里,我们定义了一个构造函数并将局部变量 name 赋值给实例变量。
@ 运算符是 this 关键字的别名,它用于指向类的实例变量。
如果我们在构造函数的参数前放置 @,则它将自动设置为实例变量。因此,上述代码可以简化为如下所示 -
class Student constructor: (@name)->
示例
这是一个 CoffeeScript 中构造函数的示例。将其保存在名为 constructor_example.coffee 的文件中。
#Defining a class
class Student
constructor: (@name)->
#instantiating a class by passing a string to constructor
student = new Student("Mohammed");
console.log "the name of the student is :"+student.name
编译代码
打开命令提示符并编译上述示例,如下所示。
c:\>coffee -c constructor_example.coffee
执行上述命令将生成以下 JavaScript 代码。
// Generated by CoffeeScript 1.10.0
(function() {
var Student, student;
Student = (function() {
function Student(name) {
this.name = name;
}
return Student;
})();
student = new Student("Mohammed");
console.log("The name of the student is :"+student.name);
}).call(this);
执行代码
通过在命令提示符上执行以下命令来运行上述示例。
coffee constructor_example.coffee
运行后,上述示例将为您提供以下输出。
The name of the student is :Mohammed
实例属性
与对象相同,我们也可以在类中拥有属性。这些被称为 实例属性。
示例
考虑以下示例。在这里,我们在类中创建了变量 (name, age) 和函数 (message()),并使用其对象访问了它们。将此示例保存在名为 instance_properties_example.coffee 的文件中。
#Defining a class
class Student
name="Ravi"
age=24
message: ->
"Hello "+name+" how are you"
#instantiating a class by passing a string to constructor
student = new Student();
console.log student.message()
编译后,上述代码生成以下输出。
// Generated by CoffeeScript 1.10.0
(function() {
var Student, student;
Student = (function() {
var age, name;
function Student() {}
name = "Ravi";
age = 24;
Student.prototype.message = function() {
return "Hello " + name + " how are you";
};
return Student;
})();
student = new Student();
console.log(student.message());
}).call(this);
静态属性
我们可以在类中定义静态属性。静态属性的作用域仅限于类内,我们使用 this 关键字 或其别名 @ 符号创建静态函数,并且必须使用类名作为 Class_Name.property 来访问这些属性。
示例
在以下示例中,我们创建了一个名为 message 的静态函数。并访问了它。将其保存在名为 static_properties_example.coffee 的文件中。
#Defining a class
class Student
@message:(name) ->
"Hello "+name+" how are you"
console.log Student.message("Raju")
打开命令提示符并使用以下命令编译上述 CoffeeScript 文件。
c:\>coffee -c static_properties_example.coffee
编译后,它将为您提供以下 JavaScript 代码。
// Generated by CoffeeScript 1.10.0
(function() {
var Student;
Student = (function() {
function Student() {}
Student.message = function(name) {
return "Hello " + name + " how are you";
};
return Student;
})();
console.log(Student.message("Raju"));
}).call(this);
在命令提示符中执行上述 CoffeeScript,如下所示。
c:\>coffee static_properties_example.coffee
执行后,上述示例将为您提供以下输出。
Hello Raju how are you
继承
在 CoffeeScript 中,我们可以使用 extends 关键字在一个类中继承另一个类的属性。
示例
以下是在 CoffeeScript 中继承的示例。在这里,我们有两个类,即 Add 和 My_class。我们在 My_class 类中继承了名为 Add 类的属性,并使用 extends 关键字访问了它们。
#Defining a class
class Add
a=20;b=30
addition:->
console.log "Sum of the two numbers is :"+(a+b)
class My_class extends Add
my_class = new My_class()
my_class.addition()
CoffeeScript 在幕后使用原型继承。在 CoffeeScript 中,每当我们创建实例时,都会调用父类的构造函数,直到我们覆盖它。
我们可以使用 super() 关键字从子类调用父类的构造函数,如下面的示例所示。
#Defining a class
class Add
constructor:(@a,@b) ->
addition:=>
console.log "Sum of the two numbers is :"+(@a+@b)
class Mul extends Add
constructor:(@a,@b) ->
super(@a,@b)
multiplication:->
console.log "Product of the two numbers is :"+(@a*@b)
mul = new Mul(10,20)
mul.addition()
mul.multiplication()
动态类
CoffeeScript 使用原型继承自动继承类的所有实例属性。这确保了类是动态的;即使在创建子类后向父类添加属性,该属性仍将传播到其所有继承的子类。
class Animal
constructor: (@name) ->
class Parrot extends Animal
Animal::rip = true
parrot = new Parrot("Macaw")
console.log "This parrot is no more" if parrot.rip
执行后,上述 CoffeeScript 将生成以下 JavaScript 代码。
// Generated by CoffeeScript 1.10.0
(function() {
var Animal, Parrot, parrot,
extend = 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; },
hasProp = {}.hasOwnProperty;
Animal = (function() {
function Animal(name) {
this.name = name;
}
return Animal;
})();
Parrot = (function(superClass) {
extend(Parrot, superClass);
function Parrot() {
return Parrot.__super__.constructor.apply(this, arguments);
}
return Parrot;
})(Animal);
Animal.prototype.rip = true;
parrot = new Parrot("Macaw");
if (parrot.rip) {
console.log("This parrot is no more");
}
}).call(this);