javascript - JavaScript 中的 "scoping function"是什么?

标签 javascript

Learning Javascript Design Patterns ,作者说

Object literals don't require instantiation using the new operator but shouldn't be used at the start of a statement as the opening { may be interpreted as the beginning of a block. Outside of an object, new members may be added to it using assignment as follows myModule.property = "someValue";

Below we can see a more complete example of a module defined using object literal notation:

var myModule = {

  myProperty: "someValue",

  // object literals can contain properties and methods.
  // e.g we can define a further object for module configuration:
  myConfig: {
    useCaching: true,
    language: "en"
  },

  // a very basic method
  saySomething: function () {
    console.log( "Where in the world is Paul Irish today?" );
  },

  // output a value based on the current configuration
  reportMyConfig: function () {
    console.log( "Caching is: " + ( this.myConfig.useCaching ? "enabled" : "disabled") );
  },

  // override the current configuration
  updateMyConfig: function( newConfig ) {

    if ( typeof newConfig === "object" ) {
      this.myConfig = newConfig;
      console.log( this.myConfig.language );
    }
  }
};

// Outputs: Where in the world is Paul Irish today?
myModule.saySomething();

// Outputs: Caching is: enabled
myModule.reportMyConfig();

// Outputs: fr
myModule.updateMyConfig({
  language: "fr",
  useCaching: false
});

// Outputs: Caching is: disabled
myModule.reportMyConfig();

Using object literals can assist in encapsulating and organizing your code and Rebecca Murphey has previously written about this topic in depth should you wish to read into object literals further.

That said, if we're opting for this technique, we may be equally as interested in the Module pattern. It still uses object literals but only as the return value from a scoping function.

我的问题: 他所说的“它仍然使用对象文字,但仅作为作用域函数的返回值。”是什么意思?

具体来说什么是“作用域函数”?

最佳答案

在 JavaScript 中,使用 var 声明的变量是“函数作用域”,而不是像其他类 C 语言中的“ block 作用域”(以及 {} 语法可能暗示的含义) )。 (更新回复: block 作用域:ES6 通过两个新的变量声明引入了 block 作用域:letconst。请参阅 https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/)

使用“范围函数”创建模块意味着使用函数来包装变量的范围和您可能使用的其他逻辑,然后返回一个包含您想要的结果的对象文字。

示例:

function personFacory(name) {
    var person = {
        name: name
    };

    if (name === 'Bob') {
        person.isBob = true;
    }

    return person;
}

@Josh 提到的 IIFE 和闭包的例子也是“作用域函数”的一个有值(value)的用途。

另一个例子:

var generateId = (function(){
   var currentId = 0; 
   return function() {
       return currentId++;
   };
})();

每次调用generateId()时,它都会返回下一个整数。

关于javascript - JavaScript 中的 "scoping function"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299815/

相关文章:

javascript - 将 jQuery 与 WinJS 绑定(bind)元素结合使用

javascript - Angular.js 倒数计时器不滴答

javascript - Jquery添加重复的数组键

javascript - 如何创建 Get 请求来获取名为 account_manager 的对象的总和?

javascript - 错误: JavaScript Function Add to class dynamically

javascript - 为什么在 JavaScript 中 "Object instanceof Function"和 "Function instanceof Object"都返回 true?

javascript - 如何区分基于多个属性的对象

javascript - 对齐动态头部和主体 - .Net MVC

javascript - 使用 AJAX 时 Django 未接收新的查询字符串

javascript - 通过 jquery .on() 函数将单击元素的属性值传递给外部函数