简体   繁体   English

使用html中要调用的方法创建javascript共享类的最佳实践

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

There are several ways, I've realized, one can create (better say 'simulate') a javascript shared (static) class which contains a bunch of shared methods that can be called from html: 我已经意识到,有几种方法可以创建(最好说“模拟”)一个javascript共享(静态)类,其中包含一堆可以从html调用的共享方法:

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

What is the best practice I wonder? 我想知道的最佳做法是什么?

You can do 你可以做

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

        },
        my_method_to_call_2 : function(){

        }
    };
    </script>

and call them by 然后打电话给他们

my_static_methods.my_method_to_call_1();

the same result can be achieved by doing 通过执行相同的结果

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