是否有一个允许您编写"真实"使用Javascript的课程?

时间:2015-02-19 07:08:01

标签: javascript

是否有框架允许您定义这样的类? 如果不是框架,是否有任何解决方法来获得这种语法?

class Car 
{
    var speed = 122; 
    var model = "Nissan Z";

    function drive() 
    {
       return doStuffToDrive();
    }
}

var car = new Car();
car.drive();

或者这是你用Javascript最接近的?

var car = {
    speed: 122,
    model: "nissan Z",
    drive: function () {
        return doStuffToDrive();
    }
}

4 个答案:

答案 0 :(得分:4)

今天的内置方式

var Car = function(opts) {
 // This is your constructor.
 this.model = opts.model;
 this.speed = opts.speed;
};

// Instance methods. By declaring them here instead of inside the ctor,
// you only have one instance of the function, called by multiple instances of
// cars, but with the car instance as the ´this´ context.
Car.prototype.drive = function() {
  console.log(this.model);
  console.log(this.speed);
};

// And creating an instance:
var car = new MyCar({
  model: 'Nissan Z',
  speed: 122
});

car.drive();

框架

有太多框架需要提及; Backbone是一个,有这个,还有其他一些东西。

var Car = Backbone.Model.extend({
  speed: 122, // default value
  model: "Nissan Z", // default value
  // Not *really* the constructor, but used as such by Backbone
  // so it can work its magic.
  initialize: function(opts) {
    this.model = opts.model;
    this.speed = opts.speed;
  },
  drive: function() {
    // Note the use of 'this'
    console.log(this.model, ' is driving', this.speed);
  }
});

// And create an instance
var car = new MyCar({
  model: 'Porsche Cayman S',
  speed: 1337
});

编译到JavaScript(ES5)

同样,有很多竞争者。

  • CoffeeScript(完全不同的语法)
  • TypeScript(语法相同,具有静态类型等附加功能)
  • 蜘蛛(差异语法)
  • ES6 / Harmony(JavaScript的下一个版本),但您需要一个名为“转换器”的工具。 Babel非常好。

明天的Bult-in方式(ES6)

class Car {
   constructor(opts) {
      this.model = opts.model;
      this.speed = opts.speed;
   }
   drive() {
     console.log(this.model);
     console.log(this.speed);
   }
}

// instance of car
var car = new Car();

答案 1 :(得分:2)

TypeScript的语法与您提出的语法非常相似。

在John Papa(http://bit.ly/1E8qC6E)的博客文章中,这是TypeScript中的示例类 -

class Car {

  engine: string;

  constructor (engine: string) {

      this.engine = engine;

  }


  start() {

      return "Started " + this.engine;

  }

} 

答案 2 :(得分:1)

类包含在EcmaScript 6规范中,您可以在BabelJS(以前称为6to5)或es6-class之类的转换器的帮助下使用它。

这里有一些ES6类语法(来自Babel学习页面)

  class SkinnedMesh extends THREE.Mesh {
  constructor(geometry, materials) {
    super(geometry, materials);

    this.idMatrix = SkinnedMesh.defaultMatrix();
    this.bones = [];
    this.boneMatrices = [];
    //...
  }
  update(camera) {
    //...
    super.update();
  }
  static defaultMatrix() {
    return new THREE.Matrix4();
  }
}

您还可以使用类似SweetJS的内容来定义类语法的宏(就像它在主页上定义的那样):

// Define the class macro here...
macro class {

  rule {

    $className {
        constructor $cparams $cbody
        $($mname $mparams $mbody) ...
    }

  } => {

    function $className $cparams $cbody

    $($className.prototype.$mname
      = function $mname $mparams $mbody; ) ...

  }

}

// And now classes are in JavaScript!
class Person {
  constructor(name) {
    this.name = name;
  }

  say(msg) {
    console.log(this.name + " says: " + msg);
  }
}
var bob = new Person("Bob");
bob.say("Macros are sweet!");

答案 3 :(得分:0)

为什么内置方式对你不够好?

function Car() {
    this.speed = 122; 
    this.model = "Nissan Z";
    this.drive = function () {
       return doStuffToDrive();
    }
}

var car = new Car();
car.drive();

console.log(typeof car)          // "object"
console.log(car instanceof Car)  // true