本文介绍了javascript 有没有类似 API 的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要用java编程,几乎没有javascript知识.
我的问题是 javascript 是否允许使用 API.例如,我有一个 javascript 应用程序,我希望像中间件 API 集一样向它公开一些功能.这个中间件也可以在 javascript 中.我想划分应用程序和中间件功能.我将同时实现应用程序和中间件.这是一个有效的用例还是我以错误的方式处理它.

I have mainly programmed in java with almost no javascript knowledge.
My question is whether javascript allows usage of APIs. For example I have a javascript application, I want some functions to be exposed to it like a middleware API set. This middleware can be in javascript too. I want to compartmentalize the application and middleware functionality. I will be implementing both the application and the middleware. Is this a valid use case or am I going about it the wrong way.

推荐答案

API 是一个抽象概念,JavaScript 与任何编程语言一样,允许定义和使用 API.

An API is an abstract concept and JavaScript, like any programming language, allows the definition and use of API.

为了定义 API,将公共函数与内部字段和函数明确分离,一种流行的模式是 模块模式基于IIFE.

To define API, with clear separation of public functions from internal fields and functions, a popular pattern is the module pattern based on a IIFE.

这是一个示例,您可以在其中查看私有数据、一个私有函数和 3 个公共函数.

Here's an example, in which you can see private data, a private function, and 3 public functions.

var countLib = (function(){
   var count = 0;
   var add = function(a) { count += a };
   return {
        getCount : function() { return count },
        increment : function() { add(1) },
        decrement : function() { add(-1) }
   }
})();

它允许你做

countLib.increment();
var count = countLib.getCount(); // gets 1

count 内部值和 add 函数未公开.

The count internal value and the add function aren't exposed.

这种模式在很多流行的库中都有使用.jQuery 中经常使用的一个变体让库在上下文中分配自己:

This pattern is used in many popular libraries. A frequent variant used in jQuery lets the library assign itself in the context :

(function(window){
   var count = 0;
   var add = function(a) { count += a };
   window.countLib = {
        getCount : function() { return count },
        increment : function() { add(1) },
        decrement : function() { add(-1) }
   }
})(window);

这有一个次要的优势,即允许缩小器减少 window 名称,它还允许库分配多个名称(例如jQuery"和$").

This has the minor advantage of enabling minifiers to reduce the window name and it also lets the library assign more than one name (for example "jQuery" and "$").

这个例子还展示了另一个有用模式的前提,命名空间:让事情变得清晰的一个重要部分是定义专用的命名空间,它是使用属性构建的:

This example shows also the premises of another useful pattern, the namespace : an essential part of making things clear is in defining dedicated name space, which are built using properties :

 myLib = myLib || {};
 myLib.mySubLib1 = (function(){
      ...
 })();

这里,许多文件可以有相同的模式:如果 myLib 不存在,它们都会在第一行初始化.

Here, many files can have the same pattern : all of them would initialize myLib at the first line if it doesn't exist.

您通常会结合这些模式来构建一个外观漂亮的库,其冲突风险最小(即不定义全局变量),并且不会暴露过多的内部结构,以便您可以使实现不断发展.

You'll usually combine those patterns to build a good looking library with minimal conflict risks (i.e. without defining global vars) and without exposing too much of its internals so that you can make the implementation evolve.

对于更复杂的 API,您还可以定义类.

For more complex API, you can also define classes.

这篇关于javascript 有没有类似 API 的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:06