正确使用JavaScript接口关键字 [英] Correct use of the JavaScript interface keyword

查看:176
本文介绍了正确使用JavaScript接口关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,不,我不是要为我的JavaScript代码创建任何类似Java的界面。我已经看到了这些问题,虽然我仍然是JavaScript的相对新手,但我的思绪在(IMO)适当的恐惧中退缩了。

First of all, no, I'm not trying to create any sort of Java-like interface for my JavaScript code. I've seen those questions all over, and while I'm still a relative novice to JavaScript, my mind recoiled in (IMO) appropriate horror at the thought.

然而,我很好奇接口关键字的实际用途是什么。例如, Math 是一个包含定义(但不是实现)的接口。我相信(并且可能完全错误)这些是为语言的定义者提供一种方法来强制执行一系列要在各种JavaScript引擎中实现的行为。这是正确的吗?

However, I'm curious what the actual intended use of the interface keyword is. For example, Math is an interface, containing definitions (but not implementations). I believe (and may be totally wrong) that these are there to provide a means for the definers of the language to enforce a set of behaviors to be implemented in various JavaScript engines. Is that correct?

此外,我希望有一个包含一堆实用方法的静态类。我喜欢Math.sqrt(3)有一个外部命名空间('Math'),它是大写的,并且有许多逻辑上类似的方法和值。也许只是我的Java / Ruby背景让我想要分组对象的资金。这是不好的形式?

Furthermore, I have a desire to have a "static class" that contains a bunch of utility methods. I like that Math.sqrt(3) has an outer namespace ('Math') which is capitalized, and a number of logically similar methods and values in it. Maybe it's just my Java/Ruby background that makes me want a capital on the grouping objects. Is that bad form?

var ShapeInspections = {
  isSymmetrical: function (s) {
    // determine if shape is symmetrical
  },
  numAngles: function (s) {
    // return the number of angles
  }
}

这是一个纯粹人为的例子,但是用这种方式命名模块是否反本用?

A purely contrived example, but is it anti-idiomatic to name the "module" this way?

推荐答案

好的,与其他答案一样,您知道关键字 interface 在Javascript世界中没有真正的用例。

Okay, so as with other answers, you know that the keyword interface has no real use case in Javascript world, yet.

您的数学示例让我怀疑您是在谈论一种名为模块模式的设计模式,广泛使用用于确定Javascript代码。有许多方法可以使代码模块化。例如,就像OddDev回答你一样,着名的 Prototype Pattern 可以以模块化方式嵌入你的代码(就像你的 Math 示例一样)。以下是 Revealing Prototype Pattern 示例以及私有变量和函数,以提供额外的灵活性:

Your Math example made me suspicous that you are talking about a design pattern, called Module Pattern, widely used for scoping Javascript code. There are many ways of making your code modular. For example just like OddDev answered you , the famous Prototype Pattern can embed your code in a modular fashion (just like your Math example). Here is the Revealing Prototype Pattern example with also private variables and functions for additional flexibility:

/* Example from: 
    http://www.innoarchitech.com/scalable-maintainable-javascript-modules */
var myPrototypeModule = (function (){

   var privateVar = "Alex Castrounis",
       count = 0;

   function PrototypeModule(name){
    this.name = name;
   }

   function privateFunction() {
      console.log( "Name:" + privateVar );
      count++;
   }

   PrototypeModule.prototype.setName = function(strName){
      this.name = strName;
   };

   PrototypeModule.prototype.getName = function(){
      privateFunction();
   };

   return PrototypeModule;     
})();

但并非全部。其他选项包括 Scoped模块模式 POJO模块模式等等。看看如何编写高度可扩展且可维护的JavaScript:模块,它有一个非常简单的但是一套完整的例子。

but that is not all. Other options include Scoped module pattern, POJO module pattern and many more. Have a look at How to Write Highly Scalable and Maintainable JavaScript: Modules, it has a very simple and yet thorough set of examples.

到目前为止,我们谈到了简单的Javascript。如果您能够在代码中使用库,那么 Requirejs CommonsJS 等一系列令人惊叹的库可以帮助您解决这个问题。 开箱即用的 功能。看看Addy Osmani关于用AMD编写模块化JavaScript,CommonJS& amp; ES Harmony

So far, we talked about plain Javascript. If you have the ability to use libraries in your code, then amazing set of libraries such as Requirejs, CommonsJS are there to help you on this with out-of-the-box functionalities. Have a look at Addy Osmani's post about Writing Modular JavaScript With AMD, CommonJS & ES Harmony.

这篇关于正确使用JavaScript接口关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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