簡體   English   中英

使用html中要調用的方法創建javascript共享類的最佳實踐

[英]Best practice for creating javascript shared class with methods to be called in html

我已經意識到,有幾種方法可以創建(最好說“模擬”)一個javascript共享(靜態)類,其中包含一堆可以從html調用的共享方法:

<html>
...
  <script>
  my_method_to_call
  </script>
...
</html>

我想知道的最佳做法是什么?

你可以做

    <script>
    var my_static_methods = {
        my_method_to_call_1 : function(){

        },
        my_method_to_call_2 : function(){

        }
    };
    </script>

然后打電話給他們

my_static_methods.my_method_to_call_1();

通過執行相同的結果

var My_static_methods = function(){
    this.my_method_to_call_1 = function(){

    };
    this.my_method_to_call_2 = function(){

    };
};
var my_static_methods = new My_static_methods();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM