Javascript - 如何从该类中调用该类中的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3541348/
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-08-23 05:05:13  来源:igfitidea点击:

Javascript - How do you call a function inside a class from within that class?

javascript

提问by Petras

I am trying to call the function MyMethod from within a object but none of the syntax below works. There must be a really obvious error below but I can't see it.

我试图从对象中调用函数 MyMethod ,但下面的语法都不起作用。下面肯定有一个非常明显的错误,但我看不到。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

   <script type="text/jscript">

     function MyObject() {

       //how do I get one of these to work??
       this.MyMethod; //does not work
       this.MyMethod(); //does not work either
       MyMethod(); //does not work either

       this.MyMethod = function () {
         alert('It works');
       }
     }

     var test = new MyObject();

   </script>

</head>
<body>

</body>
</html>

回答by Quentin

There are two main problems

有两个主要问题

  1. The MIME type is text/javascript, not text/jscript
  2. You are defining the method afteryou try to call it
  1. MIME 类型是text/javascript, 不是text/jscript
  2. 尝试调用方法后,您正在定义该方法

So:

所以:

  function MyObject() {
    this.MyMethod = function () {
      alert('It works');
    }
    this.MyMethod(); //should now work
  }

  var test = new MyObject();

回答by guy schaller

you have put the call to the private method inside the constructor of the javascript class. in that point the functions are not yet initialized

你已经把对私有方法的调用放在了 javascript 类的构造函数中。在这一点上,函数尚未初始化

but if you initialize the object like so:

但是如果你像这样初始化对象:

var test = new MyObject(); 

and then do this:

然后这样做:

test.myMethod();

it will work.

它会起作用。

回答by Eureka

The 2 ways of defining a function provide different accessibilities

定义函数的两种方式提供了不同的可访问性

First, setting it a property of the parent function, as is done in the "A" version of your script below. If you do this, the function is only usable afteryou give the definition.

首先,将其设置为父函数的属性,如下面脚本的“A”版本中所做的那样。如果这样做,则该函数只有您给出定义才能使用。

Second, defining the function with the classical approach of "function functionName() { ... }". This definition undergoes "hoisting", which means the function becomes available throughout the parent object, i.e. even above the place it is defined. You can see this in the "B" version in the sample below, based on the original poster's code. Also on https://jsfiddle.net/dnL4j30u/

其次,用经典的“function functionName() { ... }”的方法定义函数。这个定义经历了“提升”,这意味着该函数在整个父对象中变得可用,即甚至在它被定义的地方之上。您可以在下面示例中的“B”版本中看到这一点,基于原始海报的代码。同样在https://jsfiddle.net/dnL4j30u/

<script>
  function MyObject() {

    MyMethod(); 

    this.MyMethod = function() {
      console.log('It works A');
    }

    this.MyMethod(); 

    MyMethod(); 

    function MyMethod() {
      console.log('It works B');
    }

  }
  var test = new MyObject();
</script>

The output is

输出是

 It works B       (this works because the second definition gets hoisted)
 It works A
 It works B

回答by binu.py

var MyObject = function MyObject() {
       this.MyMethod = function () {
         alert('It works');
       } }

var test = new MyObject(); test.MyMethod();

The above will do. Or you can just create a constructor and call this method inside that. So at the time of object creation it will call this.MyMethod()

以上就可以了。或者您可以创建一个构造函数并在其中调用此方法。所以在创建对象时它会调用 this.MyMethod()

回答by sova

I'm pretty sure you can define methods anywhere in the file, even after you call them, but they way you are defining the method is the flaw.

我很确定您可以在文件中的任何位置定义方法,即使在您调用它们之后,但它们定义方法的方式是缺陷。

http://ejohn.org/apps/learn/#5

http://ejohn.org/apps/learn/#5

Notice that if you define a variable with an anonymous function as it's value then you are not able to call the function by name (because it doesn't have one). Instead you should name it using the convention

请注意,如果您使用匿名函数定义变量作为其值,那么您将无法按名称调用该函数(因为它没有名称)。相反,您应该使用约定命名它

function nameOfTheFunction( arguments ) {
...
}

I found the following link will to be very useful:

我发现以下链接非常有用:

http://www.dustindiaz.com/javascript-function-declaration-ambiguity/

http://www.dustindiaz.com/javascript-function-declaration-ambiguity/