什么是“范围界定功能”。在JavaScript中? [英] What is a "scoping function" in javascript?

查看:77
本文介绍了什么是“范围界定功能”。在JavaScript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Javascript设计模式 中,作者说


对象文字不需要使用new运算符进行实例化,但不应在语句开头使用
作为开头{可能是
被解释为块的开头。在对象之外,可以使用以下赋值将新的
成员添加到其中
myModule.property =someValue;



下面我们可以查看使用
对象文字表示法定义的模块的更完整示例:




  var myModule = {

myProperty:someValue,

//对象文字可以包含属性和方法。
//例如,我们可以为模块配置定义另一个对象:
myConfig:{
useCaching:true,
language:en
},

//一个非常基本的方法
saySomething:function(){
console.log(今天世界上哪里有Paul Irish?);
},

//根据当前配置输出一个值
reportMyConfig:function(){
console.log(缓存是:+(这个.myConfig.useCaching?enabled:disabled));
},

//覆盖当前配置
updateMyConfig:function(newConfig){

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

//输出:今天保罗爱尔兰人在世界的哪个地方?
myModule.saySomething();

//输出:缓存是:启用
myModule.reportMyConfig();

//输出:fr
myModule.updateMyConfig({
language:fr,
useCaching:false
});

//输出:缓存是:禁用
myModule.reportMyConfig();




使用对象文字可以帮助封装和组织
代码和Rebecca Murphey之前已经在
深度写了这个主题,如果你想进一步阅读对象文字。



那就是说,如果我们选择的话这种技术,我们可能与模块模式感兴趣的
一样。 它仍然使用对象文字,但
仅作为范围函数的返回值。


我的问题:
他的意思是它仍然使用对象文字但仅作为范围函数的返回值。



<具体什么是范围函数?

解决方案

在JavaScript中,变量是函数作用域而不是块作用域就像在其他类C语言中一样(以及{}语法可能意味着什么)。



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



示例:

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

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

退货人;
}

IIFE和封闭@Josh提及的案例也是有价值的用途范围函数。



另一个例子:

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

每次拨打 generateId()时,它将返回下一个整数。


In Learning Javascript Design Patterns, the author says

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.

My Question: What does he mean by "It still uses object literals but only as the return value from a scoping function."

Specifically what is a "scoping function"?

解决方案

In JavaScript, variables are "function scoped" not "block scoped" like in other C-like languages (and how the {} syntax might imply).

To create a module with a "scoping function" means to use a function to wrap the scope of variables and other logic you may use, and then return an object literal with the results you want.

Example:

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

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

    return person;
}

The case of the IIFE and closure @Josh mentions is also a valuable use of a "scoping function."

Another example:

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

Each time you call generateId(), it will return the next integer.

这篇关于什么是“范围界定功能”。在JavaScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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