Javascript:为什么“这个”私有函数里面指的是全局范围? [英] Javascript: why "this" inside the private function refers to the global scope?

查看:131
本文介绍了Javascript:为什么“这个”私有函数里面指的是全局范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

function A() {}    

A.prototype.go = function() {
    console.log(this); //A { go=function()}

    var f = function() {
         console.log(this);  //Window              
    };

    f();
}

var a = new A();
a.go();

为什么函数'f'中的'this'指的是全局范围?为什么它不是函数'A'的范围?

Why does 'this' inside function 'f' refers to the global scope? Why it is not the scope of function 'A' ?

推荐答案

JavaScript有一个不同的概念,特殊名称这个指的是比其他大多数编程语言都要多的
。正好有五种不同的
方式,其中的值可以用该语言绑定。

JavaScript has a different concept of what the special name this refers to than most other programming languages do. There are exactly five different ways in which the value of this can be bound in the language.

this;

在全球范围内使用时,它将简单地引用全局对象。

When using this in global scope, it will simply refer to the global object.

foo();

此处将再次引用全球对象。


ES5注意:在严格模式下,全局案例<强>不再存在。
在这种情况下,此的值将改为 undefined

ES5 Note: In strict mode, the global case no longer exists. this will instead have the value of undefined in that case.



调用方法



Calling a Method

test.foo(); 

在此示例中,将引用 test

new foo(); 

一个函数调用,前面是 new keyword作为
a构造函数。在函数内部,
引用到新创建的 对象

A function call that is preceded by the new keyword acts as a constructor. Inside the function, this will refer to a newly created Object.

function foo(a, b, c) {}

var bar = {};
foo.apply(bar, [1, 2, 3]); // array will expand to the below
foo.call(bar, 1, 2, 3); // results in a = 1, b = 2, c = 3

使用<$ c时$ c> call 或 apply Function.prototype 的方法,$ b的值$ b 调用函数内的显式设置转换为相应函数调用的第一个参数

When using the call or apply methods of Function.prototype, the value of this inside the called function gets explicitly set to the first argument of the corresponding function call.

因此,在上面的示例中,方法案例 适用, $ b $ c里面的
foo 将设置为 bar

As a result, in the above example the method case does not apply, and this inside of foo will be set to bar.


注意: 无法用于引用对象
文字内的对象。所以 var obj = {me:this} 导致 me 引用
obj ,因为仅受列出的五个案例之一的约束。

Note: this cannot be used to refer to the object inside of an Object literal. So var obj = {me: this} will not result in me referring to obj, since this only gets bound by one of the five listed cases.



普通陷阱



虽然大多数情况都有意义,但第一个被认为是另一个
错误设计了该语言,因为从不有任何实际用途。

Foo.method = function() {
    function test() {
        // this is set to the global object
    }
    test();
}

一个常见的误解是这个里面指的是 Foo ;而在
的事实上,它

A common misconception is that this inside of test refers to Foo; while in fact, it does not.

为了获得 Foo test 中,有必要在方法中创建
局部变量指 Foo

In order to gain access to Foo from within test, it is necessary to create a local variable inside of method which refers to Foo.

Foo.method = function() {
    var that = this;
    function test() {
        // Use that instead of this here
    }
    test();
}

只是一个普通的变量名,但它通常用于引用
这个。与闭包相结合,它还可以
用于传递值。

that is just a normal variable name, but it is commonly used for the reference to an outer this. In combination with closures, it can also be used to pass this values around.

在JavaScript中工作的另一件事是函数别名,即
分配方法变量。

Another thing that does not work in JavaScript is function aliasing, which is assigning a method to a variable.

var test = someObject.methodTest;
test();

由于第一种情况,现在测试就像一个简单的函数调用;因此,
里面的这个将不再引用 someObject

Due to the first case, test now acts like a plain function call; therefore, this inside it will no longer refer to someObject.

虽然这个的后期绑定起初可能看起来不是一个坏主意,但实际上,这是使原型继承有效的原因。

While the late binding of this might seem like a bad idea at first, in fact, it is what makes prototypal inheritance work.

function Foo() {}
Foo.prototype.method = function() {};

function Bar() {}
Bar.prototype = Foo.prototype;

new Bar().method();

当在一个实例上调用方法 Bar 现在将引用该
非常实例。

When method gets called on a instance of Bar, this will now refer to that very instance.

免责声明: Shamelessy从我自己的资源中偷走 http://bonsaiden.github.com/JavaScript-Garden/#function.this

Disclaimer: Shamelessy stolen from my own resources at http://bonsaiden.github.com/JavaScript-Garden/#function.this

这篇关于Javascript:为什么“这个”私有函数里面指的是全局范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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