在 JavaScript 中:类中函数和方法定义之间的语法差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14088842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:40:44  来源:igfitidea点击:

In JavaScript: Syntax difference between function & method definition within a class

javascriptfunctionmethods

提问by user1888243

The Object class has both methods and functions meaning they both are accessed through Object.nameOfMethodOrFunction(). The following question What is the difference between a method and a functionexplains the difference between a method and and a function, but it doesn't explain how to create them within an object. For example, the code below defines the method sayHi. But how do you define a function inside the same object?

Object 类同时具有方法和函数,这意味着它们都可以通过 Object.nameOfMethodOrFunction() 访问。下面的问题What is the difference between a method and a function解释了方法和函数之间的区别,但没有解释如何在对象中创建它们。例如,下面的代码定义了方法 sayHi。但是如何在同一个对象中定义一个函数呢?

var johnDoe =
{
      fName : 'John',
      lName: 'Doe',
      sayHi: function()
      {
        return 'Hi There';
      }
};

回答by Alvin Wong

The following defines two classes, ClassAand ClassB, with equal functionality but different in nature:

下面定义了两个类,ClassAClassB,具有相同的功能但性质不同:

function ClassA(name){
    this.name = name;
    // Defines method ClassA.say in a particular instance of ClassA
    this.say = function(){
        return "Hi, I am " + this.name;
    }
}

function ClassB(name){
    this.name = name;
}
// Defines method ClassB.say in the prototype of ClassB
ClassB.prototype.say = function(){
    return "Hi, I am " + this.name;
}

As shown below, they doesn't differ much in usage, and they are both "methods".

如下图,它们在用法上没有太大区别,都是“方法”。

var a = new ClassA("Alex");
alert(a.say());
var b = new ClassB("John");
alert(b.say());


So now what you mean for "function", according to the msdn link that you gave as a comment, seems that "function" is just a "static method" like in C# or Java?

那么,根据您作为评论提供的 msdn 链接,现在您对“函数”的含义是什么,似乎“函数”只是一个“静态方法”,就像在 C# 或 Java 中一样?

// So here is a "static method", or "function"?
ClassA.createWithRandomName = function(){
    return new ClassA("RandomName"); // Obviously not random, but just pretend it is.
}

var a2 = ClassA.createWithRandomName(); // Calling a "function"?
alert(a2.say()); // OK here we are still calling a method.


So this is what you have in your question:

所以这就是你的问题:

var johnDoe =
{
      fName : 'John',
      lName: 'Doe',
      sayHi: function()
      {
        return 'Hi There';
      }
};

OK, this is an Object, but obviouslynot a class.

好的,这是一个Object,但显然不是一个类。

回答by Brad Christie

var johnDoe = {
  fName: 'John',
  lName: 'Doe',
  sayHi: function(){
    function message(){ return 'Hi there'; }
    return message();
  }
};

That's about as good as you're going to get with the object declaration method of creating a 'class' in JavaScript. Just keep in mind that function is only valid within sayHi's scope.

这与您将使用在 JavaScript 中创建“类”的对象声明方法一样好。请记住,函数仅在sayHi的范围内有效。

However, if you use a function as a class structure, you have a little more flexibility:

但是,如果将函数用作类结构,则具有更大的灵活性:

var johnDoe = function(){
  this.publicFunction = function(){
  };
  var privateFunction = function(){
  };
};
var jd = new johnDoe();
jd.publicFunction(); // accessible
jd.privateFunction(); // inaccessible

(though both are really considered methods since they have access to the object's scope within).

(尽管两者实际上都被认为是方法,因为它们可以访问对象的范围)。

回答by Joeppie

Quoting Aaron with "A method is on an object. A function is independent of an object".

引用 Aaron 的话“方法在对象上。函数独立于对象”。

Logically a method is useless without a "this" defined.

从逻辑上讲,没有定义“this”的方法是无用的。

Consider this example:

考虑这个例子:

var johnDoe =
{
    fName: 'John',
    lName: 'Doe',
    sayHi: function () {
        return 'Hi There, my name is ' + this.fName;
    }
};

function sayHi2() {
    return 'Hi There, my last name is ' + this.lName;
}

//Will print Hi there, my first name is John
alert(johnDoe.sayHi());

//An undefined will be seen since there is no defined "this" in SayHi2.
alert(sayHi2());

//Call it properly now, using the oject johnDoe for the "this"
//Will print Hi there, my last name is Doe.
alert(sayHi2.call(johnDoe));