可从作为参数传递给 Event.observe() (prototype.js) 的函数内访问的变量.为什么? [英] Variables accessible from within a function passed as parameter to Event.observe() (prototype.js). Why?

查看:42
本文介绍了可从作为参数传递给 Event.observe() (prototype.js) 的函数内访问的变量.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我正在处理的站点之一中有以下可用的 JS 脚本.我想知道为什么可以从传递给 Event.observe 的函数中访问变量countryEl"和zipEl".谁能解释一下?

提前致谢!

 

解决方案

以下代码片段演示了变量v"只能从定义它的上下文访问,即外部"功能.因此,在此上下文中定义的函数也可以访问v".

函数外(){var v = '你好!';内();log('来自外部:' + v);函数内部(){log('来自内部:' + v);}}尝试 {外();log('来自全局:' + v);}赶上(e){日志(e);}

<script>函数日志{document.body.innerHTML+=s+'<br>'}</script>

更多:http://www.adequatelygood.com/JavaScript-Scoping-和-Hoisting.html.

<小时>

附加说明

内部"函数和环境的组合称为闭包".对于确切的定义,我仍然有点困惑.有些人可能会使用这个术语来指定内部"功能本身,但这听起来更像是一种误用.这是 MDN 所说的:

<块引用>

闭包是引用独立(自由)变量的函数.换句话说,闭包中定义的函数记住"了它被创建的环境.(MDN)

<块引用>

闭包是一种特殊的对象,它结合了两件事:函数和创建该函数的环境.(MDN)

I have the following working JS script in one of the sites I'm working on. I'm wondering why the variables 'countryEl' and 'zipEl' are accessible from within the function passed to Event.observe. Can anybody explain?

Thanks in advance!

    <script type="text/javascript">
        //<![CDATA[

        document.observe("dom:loaded", function() {

            var form = $('shipping-zip-form');
            var countryEl = form.down('#country');
            var zipEl = form.down('#postcode');

            Event.observe(countryEl, 'change', function () {
                var selectedValue = $(countryEl).getValue();
                if (selectedValue == 'US') {
                    zipEl.addClassName('validate-zip-us');
                }
                else {
                    zipEl.removeClassName('validate-zip-us');
                }
            });
        });
        //]]>
    </script>

解决方案

The following code snippet demonstrates that the variable "v" is only accessible from the context in which it was defined, that is to say the context of the "outer" function. Hence, functions defined inside this context can access "v" as well.

function outer () {
  var v = 'hello!';
  inner();
  log('from outer: ' + v);
  function inner () {
    log('from inner: ' + v);
  }
}

try {
  outer();
  log('from global: ' + v);
} catch (e) {
  log(e);
}

<script>function log(s){document.body.innerHTML+=s+'<br>'}</script>

More: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html.


Additional note

The combination of the "inner" function and the environment is called a "closure". I'm still a bit confused regarding the exact definition. Some might use this term to designate the "inner" function itself, but it sounds more like a misuse. Here is what MDN says:

Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created. (MDN)

A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. (MDN)

这篇关于可从作为参数传递给 Event.observe() (prototype.js) 的函数内访问的变量.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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