简体   繁体   English

如何创建可在多个项目中使用的javascript库?

[英]How to create a javascript library that can be used in multiple projects?

So I have a custom-made javascript library that I use in multiple projects. 因此,我有一个自定义的javascript库,可在多个项目中使用。 The problem is that if I make a change in one of the projects, then I have to into all the projects that use that library and replicate the changes. 问题是,如果我在其中一个项目中进行了更改,则必须进入使用该库的所有项目并复制更改。 I want to be able to make the changes in the original library and all the projects that use/reference it are automatically updated. 我希望能够在原始库中进行更改,并且所有使用/引用它的项目都将自动更新。 How do I go about doing this in Visual Studio (2013)? 如何在Visual Studio(2013)中执行此操作?

1. Facade design pattern : (Function's name VS function's implementation ) 1.外观设计模式:(函数名称与函数的实现)

You need to define contract (Called interface in JAVA) to avoid changing the name of functions . 您需要定义协定 (JAVA中的Called interface )以避免更改功能名称。

Briefly, Don't change name of functions, Change implementation's function . 简要地说, 不要更改函数名称,请更改实现的函数

2. Attach your logic to non-changebale nameSpace : 2.将逻辑附加到非变更名称空间:

var api={}; /*"api" is namespace*/
api.myFn=function(a,b){
    return a+b;
}

is Better than : 优于:

var myFn=function(a,b){
        return a+b;
    }

3. Use Anonymous function : 3.使用匿名功能:

Best practices are to wrap the code of the WHOLE library with anonymous functions. 最佳做法是使用匿名函数包装WHOLE库的代码。

 (function(api){
       api.myFn=function(a,b){ /*"myFn" is attached to nameSpace*/
            return a+b;
       };
       window.myLibrary=api; /*Publish your library to be used outside of anonymous function */
 })({}) /*{} is the value of api*/

Then in other files/other projects , you can use it like so : myLibrary.myFn(4,5) 然后, 在其他文件/其他项目中 ,您可以像这样使用它: myLibrary.myFn(4,5)

DEMO : 演示:

  (function(api){ api.myFn=function(a,b){ /*"myFn" is attached to nameSpace*/ return a+b; }; window.myLibrary=api; /*Publish your library to be used outside of anonymous function */ })({}) /*{} is the value of api*/ console.log( myLibrary.myFn(4,5) ) 

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

相关问题 如何创建可用于多个项目的图库页面? - How to create a gallery page that can be used for multiple projects? 如何在Kotlin中创建一个库并在针对javascript或java的项目中使用它? - How to create a library in Kotlin and use it from projects that target javascript or java? 如何在Netbeans 7中创建JavaScript项目? - How to create JavaScript projects in Netbeans 7? 如何为javascript库创建参考? - How can I create a reference for javascript library? 如何使用多个变量来创建多个jquery选择器? - How can multiple variables be used to create multiple jquery selectors? 如何创建 JavaScript 库 - How to create a JavaScript Library 可以将任何Javascript库与phonegap一起使用吗? - Can any Javascript library be used with phonegap? 在Google Apps脚本中定义可在多个项目中使用的全局变量 - Defining globals in Google Apps Script that can be used across multiple projects 如何在TypeScript中创建可在普通JavaScript中使用的全局名称空间/类? - How to create global namespace/class in TypeScript that can be used in plain JavaScript? 如何将 Javascript singleton 更改为可以多次使用的东西? - How to change a Javascript singleton to something that can be used multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM