是否有JavaScript的API等概念 [英] Does javascript have a concept like APIs

查看:169
本文介绍了是否有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

计数内在价值和添加功能不被暴露。

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);

这有使minifiers减少窗口名称的次要优势,这也让库分配多个名称(例如,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 "$").

这个例子也显示了另一个有用的模式,命名空间的premises:澄清事情的一个重要部分是定义专用命名空间,其使用属性构建的:

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等概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆