封装库各部分的代码构造是什么,它有什么用? [英] What is this code construct wrapping the parts of a library and what is it useful for?

查看:120
本文介绍了封装库各部分的代码构造是什么,它有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我模仿了一个库,并能够编写以下代码.此代码创建了分配了'a'功能的'c'对象.因此,要调用'a',我将不得不编写c.a().

I imitated a library and was able to write following code. This code created 'c' object to which 'a' function is assigned. So, to call 'a', I will have to write c.a().

此外,我还可以向该'c'对象添加更多功能.我想了解这段代码中发生了什么.它看起来不像普通的面向对象编程.这种javascript编程叫什么类型?

Also, I was able to add more functions to this 'c' object. I want to understand what is happening in this code. It doesn't look like normal object oriented programming. What is this type of javascript programming called?

var c = (function(c) {
    if (c === undefined) {
        c = {};
    }

    function a() {
        alert(1);
    }
    c.a = a;
    return c;
}(c));

推荐答案

这是一个模块模式.您会看到该模式的许多变体,因此了解真实情况非常重要,您不能仅仅模仿一个.

It's a module pattern. You'll see many variants of that pattern, so it's essential to understand what really happens, you can't just imitate one.

这段代码的重点是完成一个对象c(通常是您的全局库).您的应用程序中可能有许多类似的代码段,所有c的构建段,可能每个都在其自己的文件中.

The point of this piece of code is to complete an object c (typically your global library). You probably have many similar pieces of code in your application, all building pieces of c, probably each of those in its own file.

如果作为参数传递给函数的库对象c尚不存在(c === undefined),则会创建它.这样就可以不依赖于执行顺序或预先执行的文件.

In case the library object c, which is passed as argument to the function, doesn't exist yet ( c === undefined ), it is created. This makes it possible to not depend of the execution order or of a preexecuted file.

分配的右侧是 IIFE (立即调用函数表达式),这是一个立即被调用的函数.这种构造的优点是它创建了一个范围,可以在其中声明变量(例如a函数)而不会污染外部(全局)范围.这一点很重要,因为a无论如何都被外部化了,但是一个模块通常取决于几个内部(私有)函数和变量.

The right part of the assignment is an IIFE (Immediately Invoked Function Expression), that is a function which is immediately called. The advantage of this construction is that it creates a scope in which variables (for example the a function) can be declared without polluting the external (global) scope. Here the point is moot as a is externalized anyway but a module typically depends on several internal (private) functions and variables.

可能需要解释的细节:所有这些文件看起来都定义了一个新变量c,但是即使文件是串联的,这里也没有问题:var语句不会如果新变量已经存在,则不要定义它(为整个作用域定义一个变量,此处是全局的,甚至在声明之前).

A detail that might need an explanation : all those files look like they define a new variable c but there's no problem here, even if the files are concatenated : a var statements doesn't define a new variable if it already exists (a variable is defined for the whole scope, here globally, even before the point of declaration).

另一种写此方法的方法是

Another way to write this would have been

var c = c || {}; // ensure the c variable is defined, and initialize its value it if necessary

(function() { // let's use an IIFE to have a protected scope and not pollute the global one
  function a() {
    alert(1);
  }
  c.a = a; // let's augment c
})();

这个可能更清楚

  • 它明确区分了两个步骤(使用IIFE进行c初始化和c完成)
  • 它不依赖于两个同名的c变量
  • 它不太冗长
  • it explicitly separates the two steps (c initialization and c completion using an IIFE)
  • it doesn't depend on two c variables with the same name
  • it is less verbose

这篇关于封装库各部分的代码构造是什么,它有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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