在Javascript中创建类的不同方法是否有名称,它们是什么? [英] Do the different methods of creating classes in Javascript have names and what are they?

查看:43
本文介绍了在Javascript中创建类的不同方法是否有名称,它们是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到Javscript类创建了所有这些不同的方式:

I've seen Javscript classes created all of these different ways:

function Thing() {

   method1: function() {

   },
   method2: function() {

   }   
}

方法1

var thing = {

   method1: function() {

   },
   method2: function() {

   }   
};

方法2

function Thing() {

}

Thing.prototype.method1 = function() {

}

Thing.prototype.method2 = function() {

}

方法3

var thing = (function(){
   var b={};

   b.method1 = function(){

   };

   b.method2 = function class2(){

   };

   return b;
}());

方法4

这些不同的方法声明类是否有名称,它们是什么?还有我错过了任何方法吗?

Do these different methods class of declaration have names, and what are they? Also have I missed any methods?

推荐答案

首先,让我们看一下你问过哪些实际上不是制作课程的方法。

First, let's look at things you asked about which are not actually methods to make classes.

如果你只需要一个带方法但没有课程的对象,你可以创建一个对象文字

If you only need one object with methods, but no class, you can create an object literal.

var myObject = {
  varname: value,
  varname: value,
  methodname: function() {
     // do something
  },
  methodname: function() {
     // do something
  }
};

这很少有用,但它会创建一个带有自己变量的实例,其中一些是函数。请注意,在JS中,属性和方法之间没有区别。方法只是函数类型的属性。

This is rarely useful, but it creates a single instance with its own variables, some of which are functions. Note that in JS, there is no difference between a property and a method. A method is just a property of type function.

在函数内创建的变量 var 关键字仅在函数内可用。创建库的人通常以自执行函数的形式将其整个代码包装在闭包中,以便代码末尾不再存在任何临时变量:

Variables created inside a function with the var keyword are only available inside the function. People who make libraries often wrap their whole code in a closure in the form of a self-executing function, so that any temporary variables no longer exist at the end of the code:

(function(){
  // code here
}());






现在要在JS中创建一个类,你只需要需要创建一个构造函数。您可以使用关键字在创建的实例中存储属性。


Now to make a class in JS, you only need to create a constructor function. You can store properties in the created instance with the this keyword.

function MyClass(param) {
  this.value = param;
}

var instance = new MyClass("example");

函数是存储在对象中的代码块。您可以将函数存储在变量中,就像其他任何东西一样,因此您也可以将函数存储在对象中。

A function is a block of code stored in an object. You can store functions inside variables like anything else, so you can also store functions inside objects.

function MyClass() {
  this.myMethod = function() {
    alert("hi");
  };
}

但是因为你班级的所有实例都需要运行相同的代码,所以在构造函数中创建函数效率不高。这将为您创建的每个实例创建一个新的函数副本。因此,我们不是这样做,而是将函数存储在对象的原型中。

But since all instances of your class will need to run the same code, it isn't efficient to create the function inside the constructor. That will make a new copy of the function for each instance you create. So instead of doing this, we store the function in the prototype of the object.

每当你输入say时, instance.someproperty ,JS引擎查看 instance ,看它是否有一个名为 someproperty 的属性。如果没有,它会尝试查看 instance 的原型,看看它是否具有该名称的属性。如果没有,它会尝试查看原型的原型......依此类推,直到它到达链的顶端。

Whenever you type say, instance.someproperty, the JS engine looks at instance to see if it has a property called someproperty. If it doesn't, it tries to look at instance's prototype to see if it has a property of that name. And if it doesn't, it tries to look at that prototype's prototype... and so on, until it reaches the top of the chain.

所以如果你存储 MyClass.prototype.myMethod 中的函数,您将能够使用 instance.myMethod 调用它,并且只会是所有实例共享的函数的一个副本。

So if you store the function in MyClass.prototype.myMethod, you will be able to call it with instance.myMethod and there will only be one copy of the function shared by all instances.

function MyClass(param) {
  this.value = param;
}

MyClass.prototype.myMethod = function() {
  alert(this.value);
};

有一个很好的理由将函数放在构造函数中而不是使用原型链:制作伪私有变量和getter / setter函数。 在此处查看更多信息。

There is one good reason to put functions inside the constructor instead of using the prototype chain: making pseudo-private variables and getter/setter functions. See more here.

至于如何将这两种绑定方法命名为对象,在构造函数内声明的函数是特权方法,而使用原型模式声明的函数是称为公共方法。你可以在一个班级上使用它们。

As for how to name those two ways of binding methods to an object, functions declared inside the constructor are privileged methods while those declared with the prototype pattern are called public methods. You can use both on a single class.

这篇关于在Javascript中创建类的不同方法是否有名称,它们是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆