简体   繁体   English

如何用Javascript创建OOP类

[英]How to create OOP Class in Javascript

I'm hesitant to use just any tutorial because I know how those tutorials can end up being, teaching you bad ways to do things. 我不愿使用任何教程,因为我知道这些教程最终会变成什么样,并教给您错误的处理方法。 I want to setup a class in Javascript, so that I can just do 我想用Javascript设置一个类,这样我就可以

var vehicle = new Vehicle(el);
var model = vehicle->getModel();

These functions would read the HTML and get and manage the elements on the page. 这些功能将读取HTML并获取和管理页面上的元素。 I'm current doing a setup like... 我目前正在执行类似...的设置

var model = function () {
 return {
  getName: function () {

  },
  getVehicleCount: function () {

  },
  incrementCount: function (id) {
   console.log(x);
  }
 }
}();

I'm still learning classes in Javascript... I'd like to be able to pass the class a node for the element all of the methods will use, but I'm not sure I'm doing this right... 我仍在学习Java语言中的类...我希望能够将所有方法将要使用的元素的节点传递给该类,但是我不确定我是否做对了...

There is no such thing as a class in JavaScript, instead everything in JavaScript is an object. JavaScript中没有类,而是JavaScript中的所有对象都是对象。

To create a new object you define a function that uses the this keyword in it (a “constructor function”), and then call it with the new operator: 要创建一个新对象,您定义一个在其中使用this关键字的函数(“构造函数”),然后使用new运算符对其进行调用:

function Foo (id) { // By convention, constructor functions start with a capital letter
    this.id = id;
}

var foo1 = new Foo(1);
var foo2 = new Foo(2);

However, these objects have no methods. 但是,这些对象没有方法。 To add methods, you need to define a prototype object on their constructor function: 要添加方法,您需要在其构造函数上定义一个原型对象:

Foo.prototype = {
    getId: function () {
        return this.id;
    }
}

This new getId function will be usable by all Foo objects. 这个新的getId函数将对所有Foo对象都可用。 However, as was stated, there are no classes in JavaScript and as such there are other constructs you will use in order to produce different results. 但是,如上所述,JavaScript中没有类,因此将使用其他构造来产生不同的结果。

I highly recommend the videos by Douglas Crockford in which he explains much of the javascript OO nature. 我强烈推荐道格拉斯·克罗克福德(Douglas Crockford)的视频,他在其中解释了很多javascript OO性质。 The talks can be found here: 可以在这里找到讲座:

http://developer.yahoo.com/yui/theater/ http://developer.yahoo.com/yui/theater/

Douglas Crockford — The JavaScript Programming Language Douglas Crockford — JavaScript编程语言

Douglas Crockford — Advanced JavaScript Douglas Crockford —高级JavaScript

Those will give you a basic understanding of the structure of javascript and should help the transition from classical to functional programming. 这些将使您对javascript的结构有基本的了解,并应有助于从经典编程向函数式编程的过渡。

Although there are no classes in JavaScript, you can create constructor functions. 尽管JavaScript中没有类,但是您可以创建构造函数。 A constructor function works by binding methods to an objects prototype. 构造函数通过将方法绑定到对象原型来工作。 There are several ways to do this, each with advantages and disadvantages. 有几种方法可以做到这一点,每种方法都有优点和缺点。 I personally prefer the most straigthforward way, which works by appending methods to "this": 我个人更喜欢最直接的方法,该方法是将方法附加到“ this”上:

var Constructor = function() {

  //object properties can be declared directly
  this.property = "value";

  //to add a method simply use dot notation to assign an anonymous function to an object
  //property
  this.method = function () {
    //some code
  }

  //you can even add private functions here. This function will only be visible to this object methods
  function private() {
    //some code
  }
  //use return this at the end to allow chaining like in var object = new Constructor().method();
  return this; 

}

There is nothing like classes in JavaScript. JavaScript中没有像类这样的东西。 JavaScripts inheritance works with prototypes. JavaScript继承可用于原型。 You can take a look at base2 , which mimics class-like behaviour in JavaScript. 您可以看一下base2 ,它模仿了JavaScript中类似类的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM