CoffeeScript - 类和继承

JavaScript不提供关键字.我们可以使用对象及其原型在JavaScript中实现继承.每个对象都有自己的原型,并从原型继承功能和属性.由于原型也是一个对象,它也有自己的原型.

虽然原型继承比经典继承强大得多,但对新手用户来说却很困难和困惑.

CoffeeScript中的类

针对此问题,CoffeeScript提供了一个基本结构,称为,它是使用JavaScript的原型构建的.您可以使用class关键字在CoffeeScript中定义一个类,如下所示.

class Class_Name

示例

请考虑以下示例,此处我们使用关键字创建了一个名为学生的类.

class Student

如果您编译上面的代码,它将生成以下JavaScript.

var Student;

Student = (function() {
  function Student() {}

  return Student;

})();

实例化一个类

我们可以像使用其他面向对象的编程语言一样使用new运算符实例化一个类,如图所示下面.

new Class_Name

您可以实例化上面创建的(使用 new 运算符的学生课程如下所示.

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

实例属性

与对象相同,我们也可以在类中包含属性.这些被称为实例属性.

示例

请考虑以下示例.在这里,我们在类中创建了变量(名称,年龄)和函数(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关键字或其别名 @ 符号创建静态函数,我们必须使用类名访问这些属性as 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的.在这里,我们有两个类,即添加 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);