<legend id='31aIg'><style id='31aIg'><dir id='31aIg'><q id='31aIg'></q></dir></style></legend>
  • <i id='31aIg'><tr id='31aIg'><dt id='31aIg'><q id='31aIg'><span id='31aIg'><b id='31aIg'><form id='31aIg'><ins id='31aIg'></ins><ul id='31aIg'></ul><sub id='31aIg'></sub></form><legend id='31aIg'></legend><bdo id='31aIg'><pre id='31aIg'><center id='31aIg'></center></pre></bdo></b><th id='31aIg'></th></span></q></dt></tr></i><div id='31aIg'><tfoot id='31aIg'></tfoot><dl id='31aIg'><fieldset id='31aIg'></fieldset></dl></div>
      <bdo id='31aIg'></bdo><ul id='31aIg'></ul>

      <small id='31aIg'></small><noframes id='31aIg'>

    1. <tfoot id='31aIg'></tfoot>

      1. Javascript中静态函数声明和普通函数声明的区别?

        Difference between Static function declaration and the normal function declaration in Javascript?(Javascript中静态函数声明和普通函数声明的区别?)

            <i id='a3J9W'><tr id='a3J9W'><dt id='a3J9W'><q id='a3J9W'><span id='a3J9W'><b id='a3J9W'><form id='a3J9W'><ins id='a3J9W'></ins><ul id='a3J9W'></ul><sub id='a3J9W'></sub></form><legend id='a3J9W'></legend><bdo id='a3J9W'><pre id='a3J9W'><center id='a3J9W'></center></pre></bdo></b><th id='a3J9W'></th></span></q></dt></tr></i><div id='a3J9W'><tfoot id='a3J9W'></tfoot><dl id='a3J9W'><fieldset id='a3J9W'></fieldset></dl></div>
              • <bdo id='a3J9W'></bdo><ul id='a3J9W'></ul>

                <small id='a3J9W'></small><noframes id='a3J9W'>

                  <tfoot id='a3J9W'></tfoot>
                    <tbody id='a3J9W'></tbody>

                  <legend id='a3J9W'><style id='a3J9W'><dir id='a3J9W'><q id='a3J9W'></q></dir></style></legend>
                  本文介绍了Javascript中静态函数声明和普通函数声明的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有很多方法可以在 javascript 中声明一个函数.其中一种方法是声明一个类,其中的静态函数如下所示.

                  There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below.

                  class className {
                   static fucntionName() {
                   }
                  }
                  

                  另一种声明方式是通过传统的javascript样式,如下所示.

                  another way of is declaring is through the tradition javascript style as showed below.

                  function functionName() {
                  }
                  

                  我想知道使用这两种情况的优缺点.静态方法是否有任何特定用例,为什么要声明一个类(我们知道在 javascript 中不需要实例化该类来访问静态函数).为什么不在所有/任何用例中都使用传统的函数声明方式(上例中的第二种情况)?

                  I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in javascript there is no need to instantiate the class in order to access the static function). Why not just use the traditional way (the second case in the above example) of function declaration in all/any use case?

                  我想详细了解一下.

                  推荐答案

                  如你所知,static 方法可以从类本身调用.

                  A static method is callable from the class itself, as you already know.

                  静态方法可以从类的实例调用,因此您基本上必须先创建一个对象才能访问该方法.

                  A non-static method is callable from an instance of the class, so you basically have to create an object before being able to access that method.

                  例如,对于一个 addNumbers(var a, var b) 它确实 return a+b,是否真的有必要浪费内存来实例化类的对象只是添加这两个数字?不,你只需要结果,这就是静态的全部意义.

                  For example, for a addNumbers(var a, var b) which does return a+b, is it really necessary to waste memory instantiating an object of the class just to add those 2 numbers? No, you just need the result and that's the whole point of having static.

                  使用第一种样式允许您将特定类中的方法分组(想想命名空间之类的东西).也许您可以定义像 MathString 这样的类,它们都有 add 方法,但以不同的方式实现.单独调用 add() 会令人困惑,但 Math.add()String.add() 不会.

                  Using the first style allows you to group methods in a particular class (think of something like namespaces). Maybe you can define classes like Math and String, which both have the add method but implemented in a different way. Calling add() by itself would be confusing, but Math.add() and String.add() are not.

                  export 风格,另一方面,做了完全不同的事情.它允许您使用来自另一个模块的函数.

                  The export style, on the other way, does a completely different thing. It allows you to use functions from another module.

                  想一想:

                  first_module.js

                  function cube(var x) {
                      return x * x * x;
                  }
                  

                  second_module.js

                  import { cube } from 'first_module'; // <-- ERROR
                  alert( cube(3) ); // <-- Undefined function
                  

                  但是,如果你这样声明 first_module:

                  But, if you declare first_module this way:

                  export function cube(var x) {
                      return x * x * x;
                  }
                  

                  那么 second_module 就可以正常工作了.

                  Then second_module will work fine.

                  这篇关于Javascript中静态函数声明和普通函数声明的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                    <i id='wpa5r'><tr id='wpa5r'><dt id='wpa5r'><q id='wpa5r'><span id='wpa5r'><b id='wpa5r'><form id='wpa5r'><ins id='wpa5r'></ins><ul id='wpa5r'></ul><sub id='wpa5r'></sub></form><legend id='wpa5r'></legend><bdo id='wpa5r'><pre id='wpa5r'><center id='wpa5r'></center></pre></bdo></b><th id='wpa5r'></th></span></q></dt></tr></i><div id='wpa5r'><tfoot id='wpa5r'></tfoot><dl id='wpa5r'><fieldset id='wpa5r'></fieldset></dl></div>

                      1. <tfoot id='wpa5r'></tfoot>
                          <tbody id='wpa5r'></tbody>
                      2. <small id='wpa5r'></small><noframes id='wpa5r'>

                        • <bdo id='wpa5r'></bdo><ul id='wpa5r'></ul>

                            <legend id='wpa5r'><style id='wpa5r'><dir id='wpa5r'><q id='wpa5r'></q></dir></style></legend>