简体   繁体   English

如何创建和实现自定义JavaScript库

[英]How to create and implement a custom JavaScript library

I am new to javaScript and am unsure how to go about creating a new js library and referencing this in another js file. 我是javaScript的新手,不确定如何创建新的js库并在另一个js文件中引用它。

If I have a standalone file Utilities.js 如果我有一个独立文件Utilities.js

var Utilities= 
{
   show: function(input)
   {
      alert(input);
   }
};

Am I missing something as to how a library should be defined standalone? 我是否缺少关于如何独立定义库的内容?

My second question is how to use that is sub-sequent js files. 我的第二个问题是如何使用后续的js文件。 What I did so far is: 到目前为止,我所做的是:

<script type="text/javascript" src="resources/Utilities.js"></script>

In my index.html. 在我的index.html中。 Is this enough to reference it as: 这足以将其引用为:

Utilities.show("Hello");

In any other java script file? 在其他Java脚本文件中吗?

I tested it in this fashion and got and error "Utilities is not defined" 我以这种方式对其进行了测试,但出现错误“未定义实用程序”

NOTE: This is just an example and not my full and practical purpose. 注意:这仅是示例,并非我的全部和实际目的。

  1. It is OK to use object literals, but you can define libraries using other patterns (module, revealing module, constructors, etc). 可以使用对象文字,但是您可以使用其他模式(模块,显示模块,构造函数等)定义库。

I recommend these links to understand primitives, scopes, closures, object literals, etc. 我建议使用这些链接来了解原语,范围,闭包,对象文字等。

http://bonsaiden.github.io/JavaScript-Garden/ http://bonsaiden.github.io/JavaScript-Garden/

http://jsbooks.revolunet.com/ http://jsbooks.revolunet.com/

  1. To call the method inside index.html you need to add a tag. 要在index.html中调用该方法,您需要添加一个标签。

    <script> Utilities.show("Hello"); </script>

But this approach it's not recommended. 但是不推荐这种方法。 Instead of it, you can create a new JS file to run your library code. 代替它,您可以创建一个新的JS文件来运行您的库代码。

<script type="text/javascript" src="resources/Utilities.js"></script> <script type="text/javascript" src="resources/main.js"></script>

In main.js 在main.js中

Utilities.show("Hello");

Hope it helps. 希望能帮助到你。

Yes, including that Javascript file with that global variable declared is enough to call your methods this way Utilities.show("Hello"); 是的,包括声明了全局变量的Javascript文件足以以这种方式调用您的方法Utilities.show("Hello"); from another Javascript file loaded after Utilities.js or inside a <script></script> section of your html. 加载另一个JavaScript文件Utilities.js或里面<script></script>您的HTML的部分。

But you can actually improve it a little, following the module pattern and exposing only the functions you really need to the global scope (you'll likely write some functions that the users of your library should not call directly, this allows you to do it in a clean way): 但是您实际上可以对其进行一些改进,遵循模块模式并将仅需要的功能公开给全局范围(您可能会编写一些库用户不应该直接调用的功能,这使您可以做到这一点以一种干净的方式):

var Utilities=Utilities||(function () {

   //Accessible only here
   var privateArray=[];

   //Cannot be called from outside this function
   var privateFunction=function(){
   }

   //Return only what must be publicly accessible, in this
   //case only the show() method
   return {
      show: function(input){
         privateFunction();
         alert(input);
      }
   }
})();

That (function () { /* Code */})(); 那个(function () { /* Code */})(); , defining a new scope for your code will also avoid name clashes with other global javascript object/functions. ,为您的代码定义新范围也将避免名称与其他全局javascript对象/函数冲突。

Given the fact that you gave, within yout question, zero context of what you're trying to achieve, the best answer to your original question is that it depends . 鉴于您在自己的问题内给出了要实现的目标的零上下文,因此,对原始问题的最佳答案是it depends

If you just need a bunch of files and you're done (like in your example, Utilities.js and a few more) then you're ok with the way you're heading to. 如果您只需要一堆文件并且已经完成(例如您的示例中的Utilities.js等),那么您就可以确定前进的方向。

But of course, you'll allways want to scale your front end and thus you should adhere to some architectural pattern. 但是,当然,您总会想要扩展前端,因此您应该遵循某种架构模式。 So, if you're building a client side (browser-side) application, then you should really implement your libraries using the module pattern, and begin your project from a good project example / scaffold. 因此,如果您要构建客户端(浏览器端)应用程序,那么您应该真正使用模块模式来实现您的库,并从一个好的项目示例/支架开始您的项目。

On the other hand, if you're rendering the html on server (eg you're using PHP to render the final html file that will be sent to browser) and you just need some thin functionality in the browser, the way you begun can be okay if you're careful. 另一方面,如果要在服务器上呈现html(例如,您正在使用PHP呈现将发送到浏览器的最终html文件),而您只需要浏览器中的一些精简功能,那么开始的方式可以小心一点就可以了。 Also, you can still implement the module pattern here too, although I strongly suggest that you should make use of namespacing to have a clear separation of concerns. 此外,尽管我强烈建议您应该使用命名空间来明确关注点,但是您仍然可以在此处实现模块模式。

In browser based javascript you can't just call functions from different files yet. 在基于浏览器的javascript中,您还不能仅从其他文件中调用函数。 In Es6 there are ways. 在Es6中,有很多方法。 But not yet. 但尚未。 Which mean just because you have some variable or function etc then you cant reference it automatically in another file. 这意味着仅仅因为您有一些变量或函数等,您就无法在另一个文件中自动引用它。

Unless both files are loaded into one html and are loaded in order. 除非将两个文件都加载到一个html中并按顺序加载。

Alternatively you could run task runner like grunt and 'merge' them upon each build. 另外,您可以像grunt一样运行任务运行程序,并在每次构建时“合并”它们。

Javascript doesnt have special concept of library, in es6 it's a little different, everything is an object. Javascript没有特殊的库概念,在es6中有点不同,一切都是对象。

What you are doing is just creating an object. 您正在做的只是创建一个对象。 and yes it will work. 是的,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM