简体   繁体   English

闭包javascript vs Java

[英]Closures javascript vs java

I am learning javascript and I came across the following code snippet: 我正在学习javascript,并且遇到了以下代码段:

var outerValue = true;

function outerFn(){
  assert( outerFn && outerValue, "These come from the closure." );
}

Insofar as I understand closures in the above context, they allow the outerFn to actually see the outerValue variable. 就我在上述上下文中了解的闭包而言,它们允许outerFn实际看到outerValue变量。

My question then is: how is this any different from any other programming language - such as java for instance? 然后我的问题是:这与任何其他编程语言(例如Java)有什么不同? It is just expected that outerValue's scope will allow outerFn to see it. 只是预期externalValue的范围将允许externalFn看到它。

added later on: 稍后添加:

    var outerValue = true;
    function outerFn() {
        console.log(outerValue);
    }

    function anotherFunction(arg){
        console.log("anotherFunction");
        arg.call(this);
    }

    anotherFunction(outerFn); 

Is this then a better example of a closure? 那么这是一个更好的关闭示例吗?

Understand "clousures" as the ability to change the scope of execution of a function or method. 将“习惯”理解为改变功能或方法的执行范围的能力。 Here's an example where we run the same function by modifying the scope for 'client1' that 'May' and client2 which is 'John'. 这是一个示例,其中我们通过修改“ may”和“ client2”(即“ John”)的“ client1”范围来运行相同的功能。 This is not possible in Java. 在Java中这是不可能的。

<html>
<head>
    <script type="text/javascript" src="jquery-1.5.2.min.js"></script>
    <script type="text/javascript" >

    function assert(condition, message) {
        if (condition) {
            alert(message);
        }
    }

    function testClousures() {
        var client1 = {name: 'Mary', code: 123};
        var client2 = {name: 'John', code: 234};

        function outerFn(){ 
            assert( this.name == 'John', "These come from the closure." );
        }

        // Testing if client is John 
        outerFn.apply(client1);  // Fail
        outerFn.apply(client2);  // Success

    }

    function domReady() {
        $('#btn').click(function(){
            testClousures();
        });
    }
    </script>
</head>
<body onload="domReady()">
<br/>
<input id="btn" type="button" value="Test"></input>
</body>
</hmtl>

Imagine the function outerFn() was passed as a callback. 想象一下函数outerFn()作为回调传递。 Sometime later when it runs, even though outerValue will have fallen out of scope, it will still be accessible within the closure. 稍后,当它运行时,即使outerValue超出了范围,它仍然可以在闭包中访问。

HTH HTH

Your example does not really illustrate the difference, as you do not define the scope of outerValue. 您的示例并未真正说明差异,因为您没有定义externalValue的范围。 In Javascript, you may nest functions arbitrarily within one another, and closures make sure that inner functions can see outer functions even when invoked after the outer functions are no longer in scope. 在Javascript中,您可以在函数之间任意嵌套,并且闭包确保即使在外部函数不在范围内之后调用内部函数,也可以看到外部函数。

In java, nesting functions is not (yet) legal, and thus closures do not even come into play. 在Java中,嵌套函数尚不合法,因此闭包甚至不起作用。 Having a class field in place of outerValue and a class method in place of your function is different, as the field of course is associated with the scope of the class, not the method. 用类字段代替outerValue和用类方法代替函数是不同的,因为该字段当然是与类的范围而不是方法关联的。

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

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