JS:嵌套函数获取外部函数参数的副本? [英] JS: Nested functions gain a copy of the outer function's parameters?

查看:37
本文介绍了JS:嵌套函数获取外部函数参数的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,通过以下代码,我向自己证明了嵌套函数确实获得了外部函数参数的副本:

 var o = {};(功能(a,b,c,x){x.f = 函数(){返回 a.toString()+b.toString()+c.toString();}})(7,4,2,o);的();

代码产生

<代码> 742

这意味着 o.f 函数从匿名函数中获得 a、b 和 c 的副本.否则,我只会得到 undefinedundefinedundefined

我的问题是,

  • 首先,这总是正确的吗?还是有严格的条件?(比如,它必须是一个对象吗?等等)
  • 此外,在 javascript 中还有哪些其他类型的模糊作用域存在?我很想知道(即第三次迭代怎么样?)
  • 最后,我非常愿意阅读一份解释有关 javascript 范围的高级概念的文档.有人知道有什么好的吗?

解决方案

您观察到的称为词法范围.这意味着 JavaScript 中某个范围内的变量的绑定是由变量在代码中出现的位置决定的.它总是正确的,并且在任何层面上都是正确的.唯一的主要例外是 this 值,它是动态范围而不是词法范围的.动态范围意味着函数中的变量取决于调用函数的方式和时间.(参见词法范围和动态范围.)

示例:

var o = {一:函数(){无功 x = 5;console.log(this, x);函数 b() {console.log(this, x);}b();}};o.a();

这个例子的结果是:

{Object o} 5{窗口} 5

换句话说,第一个 console.log 将记录 this 作为对 o 对象的引用,而第二个 console.log 将记录 this 作为对 window 对象的引用.但是,两者都将 x 记录为等于 5.this 的值是 window 在没有上下文的非严格模式下调用时.所以 this 不是词法范围的,但其他变量,如 x 是.要了解有关 this 行为的更多信息,请参阅 this 的简短概述.

<小时>

直接回答您的问题:

1.首先,这总是正确的吗?还是有严格的条件?(比如,它必须是一个对象吗?等)

是的,除了 thisarguments 之外都是如此,它们会根据函数的调用方式而变化.它不必是一个对象,所有变量都是词法范围的.

<小时>

2.另外,在 javascript 中还存在哪些其他类型的模糊范围?我很想知道(即第三次迭代怎么样?)

你可以随意深入——内部函数始终可以访问其外部函数的变量.

function a() {无功x = 1;console.log('a:', x);返回函数 b() {变量 y = 2;console.log('b:', x, y);返回函数 c() {console.log('c:', x, y);};};}var foo = a();//=>记录a:1"var bar = foo();//=>日志'b:1 2'酒吧();//=>记录c:1 2"

这实际上是另一个主题的一部分,称为 闭包,当您从另一个函数中返回一个函数时会发生这种情况.

<小时>

3.最后,我很乐意阅读一份解释有关 javascript 范围的高级概念的文档.有人知道有什么好的吗?

我已经链接了一些资源.另一个不错的:

MDN:函数和函数范围(特别是嵌套函数和闭包)部分.>

此外,阅读有关闭包的任何内容都会使您受益,并且您可能还想查找词法范围.

So with the following code, I proved to myself that nested functions do indeed gain a copy of the parameters of the outer function:

    var o = {};
    (function(a,b,c,x){
        x.f = function(){
            return a.toString()+b.toString()+c.toString();
        }
    })(7,4,2,o);
    o.f();

The code yields

    742

Which means that the o.f function gains a copy of a,b, and c from the anonymous function. Otherwise, I would just get undefinedundefinedundefined

My questions are,

  • First, is this always true? or are there strict circumstances? (Like, must it be an object? etc.)
  • Also, what other kinds of obscure scopes exist like this in javascript? I'd love to know (i.e. what about third iterations?)
  • Lastly, I'd be perfectly fine with reading a document that explicates advanced concepts on javascript scopes. Does anyone know of any good ones?

解决方案

What you observe is called lexical scope. It means that the bindings of the variables in a certain scope in JavaScript are determined by where the variables appear in the code. It is always true, and it is true up to any level. The only main exception is the this value, which is dynamically scoped rather than lexically scoped. Dynamic scope means variables in the function depend on how and when the function is called. (See Lexical Scoping and Dynamic Scoping.)

Example:

var o = { 
    a: function() {
        var x = 5;
        console.log(this, x);
        function b() {
            console.log(this, x);
        }
        b();
    }
};

o.a();

The result of this example will be:

{Object o} 5
{window} 5

In other words, the first console.log will log this as a reference to the o object, while the second console.log will log this as a reference to the window object. However, both will log x as being equal to 5. The value of this is window when it is called in non-strict mode without a context. So this is not scoped lexically, but other variables, like x are. To read more about the behavior of this see A Short Overview of this.


To answer your questions directly:

1. First, is this always true? or are there strict circumstances? (Like, must it be an object? etc.)

Yes, it's true with the exception of this and arguments which change based on how the function is called. It doesn't have to be an object, all variables are lexically scoped.


2. Also, what other kinds of obscure scopes exist like this in javascript? I'd love to know (i.e. what about third iterations?)

You can go as deep as you want -- inner functions can always access the variables of their outer functions.

function a() {
    var x = 1;
    console.log('a:', x);
    return function b() {
        var y = 2;
        console.log('b:', x, y);
        return function c() {
            console.log('c:', x, y);
        };
    };
}

var foo = a();   // => logs 'a: 1'
var bar = foo(); // => logs 'b: 1 2'
bar();           // => logs 'c: 1 2'

This is actually part of another topic referred to as closures, which occur when you return a function from within another function.


3. Lastly, I'd be perfectly fine with reading a document that explicates advanced concepts on javascript scopes. Does anyone know of any good ones?

I've linked to a couple resources already. Another good one:

MDN: Functions and function scope (specifically the section on Nested Functions and Closures).

Also, you would be benefited from reading anything on closures, and you may also want to look up lexical scope.

这篇关于JS:嵌套函数获取外部函数参数的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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