Javascript返回冒号 [英] Javascript return with colon

查看:173
本文介绍了Javascript返回冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JavaScript并遇到以下结构:

I am learning JavaScript and have come across of the structure below:

var Test = (function () {

  function func1() {
      //do something.....
  }

  function func2() {
      //do something.....
  }

  function func3() {
      //do something.....
  }

  return {
      func1: func1,
      func2: func2,
      func3: func3
  };

})();

我想知道返回块在做什么。这是一个非常常用的JavaScript结构吗?请告诉我在哪里可以获得更多相关信息。

I am wondering what the return block is doing. Is this a very commonly used JavaScript structure? Please let me know where can I get more information about this.

推荐答案

这是揭示模块模式

返回的对象包含对IIFE内定义的函数的引用。因此,内部定义的函数是私有到匿名函数。

The returned object contains references to the functions defined inside the IIFE. So the functions defined inside are private to the anonymous function.

但是如果你想使用外部的内部函数,你可以使用返回的对象。

But if you want to use the inner functions outside, you can use the returned object.

测试的值将是

var Test = {
    func1: func1,
    func2: func2,
    func3: func3
};

你可以从外面拨打 func1 作为

Test.func1();

这是Javascript 模仿的方式 。由于没有使用模块模式的可见性说明符,变量/方法可以是公共/私有。

This is the way Javascript emulate class. As there is no visibility specifiers using Module pattern, variables/methods can be make public/private.

揭示模块模式的灵感来自模块模式。在揭示模块模式时,仅在对象中返回对私有变量/方法的引用。

The revealing module pattern is inspired from Module pattern. In revealing module pattern, only reference to the private variables/methods is returned in an object.

主要模式背后的想法是避免邪恶的全局变量。这看起来类似于IIFE,除了返回对象而不是函数。 IIFE内定义的变量/方法是函数的私有。要访问IIFE内的任何变量/方法,需要在返回的对象中添加它,然后可以从IIFE外部访问它。这种模式利用了闭包,因此即使在返回对象之后,也可以访问IIFE中定义的变量/方法。

The main idea behind the pattern is avoiding evil global variables. This looks similar to IIFE except an object is returned instead of function. The variables/methods defined inside the IIFE are private to the function. To access any variable/method inside the IIFE, it needs to be added in the returned object and then it can be accessed from outside of IIFE. This pattern takes advantage of closures, so the variables/methods defined inside the IIFE are accessible even after the object is returned.

来自Addy Osmani的预订学习Javascript设计模式

From Addy Osmani's book Learning Javascript Design patterns


由于Heilmann对我们想要从另一个公共方法调用一个公共方法或访问公共变量时必须重复主对象的名称这一事实感到沮丧,因此出现了Revealing Module模式。他也不喜欢Module模式的要求,因为他希望公开的东西必须切换到对象文字符号。

The Revealing Module pattern came about as Heilmann was frustrated with the fact that he had to repeat the name of the main object when we wanted to call one public method from another or access public variables. He also disliked the Module pattern’s requirement for having to switch to object literal notation for the things he wished to make public.

他努力的结果是我们更新的模式将简单地定义私有范围内的所有函数和变量,并返回一个匿名对象,其中包含指向我们希望公开的私有功能的指针。

The result of his efforts was an updated pattern where we would simply define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.

优势:


  1. 封装。 IIFE中的代码是从外部世界封装的。

  2. 清洁,有组织和可重复使用的代码

  3. 隐私。它允许创建私有变量/方法。无法从IIFE外部触及私人变量/方法。

缺点:


  1. 如果私有函数引用公共函数,则无法覆盖该公共函数

进一步阅读:


  1. https://en.wikipedia.org/wiki/Module_pattern

  2. https://carldanley.com/js-revealing-module-pattern/

  3. 如何在JavaScript中使用Revealing模块模式

  1. https://en.wikipedia.org/wiki/Module_pattern
  2. https://carldanley.com/js-revealing-module-pattern/
  3. How to use Revealing module pattern in JavaScript

编辑

来自comment 来自 @Mike


这是注意,创建一个对象是常见的(例如, var me = {}; ),然后在其上声明可能的公共成员( me.func1 = function(){/ * ... * /}; ),最后返回该对象( return me; ) 。这避免了我们在OP代码的返回语句中看到的重复(其中重复所有公共内容)。

It's of note that it's common to create an object (eg, var me = {};) and then declare the would-be public members on it (me.func1 = function() { /* ... */ };), returning that object at the end (return me;). This avoids the repetition that we see in the return statement of OP's code (where all the public stuff is repeated).

这篇关于Javascript返回冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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