没有黑客攻击,私有JavaScript函数无法访问'this'对象? [英] 'this' object can't be accessed in private JavaScript functions without a hack?

查看:159
本文介绍了没有黑客攻击,私有JavaScript函数无法访问'this'对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目一段时间,试图找出我做错了什么,当我最终将错误缩小到下面的代码无法正常工作的事实时:

I was working on a project for a while, trying to figure out what I was doing wrong, when I finally narrowed "the bug" down to the fact that the below code doesn't work as I expected:

function Alpha()
    {
    this.onion = 'onion';

    function Beta()
        {
        alert(this.onion);
        }

    Beta();
    }

alpha1 = new Alpha();
// Alerts 'undefined'

但是,如果我将代码更改为:

However, if I change the code to this:

function Alpha()
    {
    var self = this;
    this.onion = 'onion';

    function Beta()
        {
        alert(self.onion);
        }

    Beta();
    }

alpha1 = new Alpha();
// Alerts 'onion'

它的效果与我预期的一样。在浪费了我生命中的大部分时间之后,有人可以解释为什么它会这样吗?

it works like I would expect. After wasting a large portion of my life, can anyone explain why it works like this?

推荐答案

这样的工作因为每个函数都有关联自己的执行上下文

Works like this because each function has associated its own execution context.

但是还有其他方法可以做到这一点,例如:

However there are other ways to do it, for example:

使用 致电 申请 调用该函数:

Using call or apply to invoke the function:

function Alpha() {
  this.onion = 'onion';

  function Beta() {
    alert(this.onion);
  }

  Beta.call(this);
}

var alpha1 = new Alpha();
// Alerts 'onion'

新的ECMAScript第5版标准,介绍了一种方法保留函数 context Function.prototype.bind 方法:

The new ECMAScript 5th Edition Standard, introduces a way to persist the function context, the Function.prototype.bind method:

function Alpha() {
  this.onion = 'onion';

  var Beta = function () {
    alert(this.onion);
  }.bind(this);

  Beta();
}

var alpha1 = new Alpha();
// Alerts 'onion'

我们可以说 Beta 函数绑定,无论你如何调用它,它的这个值都将是完整的。

We can say that the Beta function is bound, and no matter how you invoke it, its this value will be the intact.

此方法尚未广泛支持,目前只有IE9pre3包含它,但您可以包含一个实现,使其现在可以正常工作。

This method is not widely supported yet, currently only IE9pre3 includes it, but you can include an implementation to make it work now.

现在让我们我详细说明这个的工作方式:

Now let me elaborate about how this works:

this 每个执行上下文中都存在值,并且当a a时,函数代码会隐式设置进行函数调用时,该值取决于引用的形成方式。

The this value exist on each execution context, and for Function Code is set implicitly when a function call is made, the value is determined depending how the reference if formed.

在您的示例中,当您调用 Beta(); ,因为它没有绑定到任何对象,我们可以假设引用没有基础对象,则值将引用全局对象。

In your example, when you invoke Beta();, since it is not bound to any object, we can say that the reference has no base object, then, the this value will refer to the global object.

当您调用绑定为对象属性的函数时会发生其他情况,例如:

Other case happens when you invoke a function that is bound as a property of an object, for example:

var obj = {
  foo: function () { return this === obj;}
};
obj.foo(); // true

如您所见,调用的引用 obj.bar (); 包含一个基础对象,它是 obj ,里面的这个值被调用的函数将引用它。

As you can see, the reference being invoked obj.bar(); contains a base object, which is obj, and the this value inside the invoked function will refer to it.

注意引用类型是一个抽象概念,为语言实现目的而定义,您可以在规范中看到详细信息。

Note: The reference type is an abstract concept, defined for language implementation purposes you can see the details in the spec.

隐式设置值的第三种情况是使用 new 运算符,它将引用一个新创建的对象,该对象继承自其构造函数的原型:

A third case where the this value is set implicitly is when you use the new operator, it will refer to a newly created object that inherits from its constructor's prototype:

function Foo () {
  return this; // `this` is implicitly returned when a function is called 
}              // with `new`, this line is included only to make it obvious

var foo = new Foo();
foo instanceof Foo; // true
Foo.prototype.isPrototypeOf(foo); // true

这篇关于没有黑客攻击,私有JavaScript函数无法访问'this'对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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