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
在这里,我们定义了一个构造函数,并将局部变量名赋给了实例变量。
@ 运算符是 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

实例属性

和对象一样,我们也可以在类中拥有属性。这些被称为 实例属性

示例

考虑以下示例。在这里,我们在类中创建了变量(名称、年龄)和一个函数(消息())并使用其对象访问它们。将此示例保存在名为 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 中的继承示例。在这里,我们有两个类,分别是 AddMy_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);  
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4