简体   繁体   English

具有作为对象成员的功能的闭包

[英]Closures with functions as object members

My question sounds simple: does a function defined as an object member have closure over function's scope which contains the object? 我的问题听起来很简单:定义为对象成员的函数是否具有包含对象的函数范围的闭包?

For example: 例如:

    function foo() {
      /* some
       *  code*/

     var obj = {
            prop1: //something;
            prop2: //something;
            someCoolProp: function() {
                  //code
             }
     }
}

Does the someCoolProp function reference have closure over the scope of foo ? someCoolProp函数引用是否具有foo范围的闭包? Where is the anonymous function defined so if we gave it a name where would it be accessible? 匿名函数在哪里定义,如果我们给它一个名称,哪里可以访问?

Thanks. 谢谢。

My question sounds simple: does a function defined as an object member have closure over function's scope which contains the object? 我的问题听起来很简单:定义为对象成员的函数是否具有包含对象的函数范围的闭包?

Does the someCoolProp function reference have closure over the scope of foo ? someCoolProp函数引用是否具有foo范围的闭包?

Yes. 是。 It being defined within the object initializer doesn't have any effect at all on what it closes over. 它在对象初始值设定项中定义,对它关闭的内容完全没有任何影响。 It still closes over the context of the call to foo that created it. 它仍然会在创建它的foo调用的上下文中关闭。

Where is the anonymous function defined so if we gave it a name where would it be accessible? 匿名函数在哪里定义,如果我们给它一个名称,哪里可以访问?

As of ES2015, that function does have a name: someCoolProp . 从ES2015开始,该函数确实有一个名称: someCoolProp (ES2015 added a lot of function name inference to the specification; a large number of "anonymous function expressions" no longer create anonymous functions, amusingly.) That name isn't added to any execution context's binding object (loosely, "scope"), though; (ES2015在规范中添加了很多函数名推断;大量的“匿名函数表达式”不再创建匿名函数,这很有趣。)该名称不会添加到任何执行上下文的绑定对象(松散地,“范围”)但是; the only reference to the function that exists is on the object's property of the same name. 对存在的函数的唯一引用是在同名对象的属性上。

But if you mean, if you used a named function expression: 但是,如果您的意思是,如果您使用了命名函数表达式:

var obj = {
    prop1: //something;
    prop2: //something;
    someCoolProp: function someNameHere() {
    //                     ^^^^^^^^^^^^---------------- added
        //code
    }
}

...the situation is the same: Because it's a function expression , the name isn't added to the context where it's defined. ...情况是一样的:因为它是一个函数表达式 ,所以名称不会添加到定义它的上下文中。 (Whereas with a function declaration , it is.) (而使用函数声明 ,它是。)

My question sounds simple: does a function defined as an object member have closure over function's scope which contains the object? 我的问题听起来很简单:定义为对象成员的函数是否具有包含对象的函数范围的闭包?

Yes, it has. 是的,它有。

 function foo() { var bar = 42, obj = { prop1: 'something', prop2: 'something', someCoolProp: function() { console.log(bar); } }; return obj; } foo().someCoolProp(); 

When you declare a function a new scope is created, that's means all property inside it are private so none other scope can call these property. 当您声明一个function会创建一个新的作用域,这意味着它内部的所有属性都是私有的,因此没有其他作用域可以调用这些属性。 Let me explain with different examples because it's really important to understand this. 让我用不同的例子来解释,因为理解这一点非常重要。


Here, you can't call x in the window scope, x is only inside the foo() scope 在这里,你不能在window范围内调用xx只在foo()范围内

 function foo() { var x = 'Hey'; function transformX( val ){ // I can call x because transformX is inside Foo() x = 'Ho'; } return transformX(x); } console.log( foo() ); // 'Ho' console.log( x ); // Reference Error can't read x 


Here it's a ****** bad practice, I declare y without the var keyword, so at first window doesn't know y but during bar() execution y is declared & attached inside the window scope (default behavior) ! 这是一个不好的做法,我声明y没有var关键字,所以在第一个window不知道y但是在bar()执行y被声明并附加在window范围内(默认行为)! Always use a keyword declaration : var | 始终使用关键字声明: var | let | let | 'const` “const`

 function bar() { y = 'Hey'; function transformX( val ){ y = 'Ho'; } return transformX(y); } console.log( y ); // 'Error y is not defined' bar(); console.log( y ); // 'Ho' 


This example is pretty hard than before, you can see a people variable inside the peopleObject and inside the window object. 这个例子比以前困难得多,你可以在peopleObjectwindow对象里面看到一个people变量。 This practice is good to keep some variables like private. 这种做法很好地保留了一些像私有的变量。

 var peopleObject = function(){ var people = { name: 'Yann', age: 25 } function sayMyName(){ console.log('Hi', people.name); } function getName(){ return people.name; } return { name : getName, p: people }; }; var people = peopleObject(); console.log( people.name() ); console.log( people.p ); console.log( people.sayMyName() ); // error sayMyName is not a function 

Hi hope it's gonna help you :) 嗨希望它会帮助你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM