目录

CoffeeScript - 类和继承( Classes and Inheritance)

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

尽管原型继承比经典继承强大得多,但对于新手用户来说却很困难和困惑。

CoffeeScript中的类

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

class Class_Name

例子 (Example)

考虑以下示例,这里我们使用关键字class创建了一个名为Studentclass

class Student
<p></p>

如果编译上面的代码,它将生成以下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)->

例子 (Example)

以下是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

Compiling the code

打开命令提示符并编译上面的示例,如下所示。

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);

Executing the Code

通过在命令提示符下执行以下命令来运行上面的示例。

coffee constructor_example.coffee

在运行时,上面的示例为您提供以下输出。

The name of the student is :Mohammed

实例属性

与对象相同,我们也可以在类中具有属性。 这些被称为instance properties

例子 (Example)

请考虑以下示例。 在这里,我们在类中创建了变量(名称,年龄)和函数(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 keyword或其别名@符号创建静态函数,我们必须使用类名作为Class_Name.property来访问这些属性。

例子 (Example)

在下面的示例中,我们创建了一个名为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

继承 (Inheritance)

在CoffeeScript中,我们可以使用extends关键字继承另一个类中一个类的属性。

例子 (Example)

以下是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);  
↑回到顶部↑
WIKI教程 @2018