【问题标题】:About this keyword bests uses in javascript关于这个关键字 bests used in javascript
【发布时间】:2013-10-18 15:20:20
【问题描述】:

我有几个关于在 javascript 上使用“this”关键字的问题。


  1. 是不是不断地使用this,会导致整个函数对象重载到RAM内存中?
  2. 最好申报

MyClass = function(){ 
    this.name = "Petter";
    this.age = 12;
    this.toString = function(){
        return "Name: "+this.name + " Age: " + this.age;  
    }
}

而不是


MyClass = function(){
    var _this = this;
    _this.name = "Petter";
    _this.age = 12;
    _this.toString = function(){
        return "Name: "+_this.name + " Age: " + _this.age;  
    }
}

或者你有什么可以推荐给我的?

【问题讨论】:

标签: javascript oop this


【解决方案1】:

不,这不是真的,我从来没有听说过这样的事情。

当你做的时候你所做的一切

var _this = this;

正在创建一个变量以指向内存中使用this 时引用的对象。不管你说:

this.name = "Petter";

_this.name = "Petter";

您仍在为同一个对象分配一个属性。您引用该对象的方式(_thisthis)没有区别。

编辑

当您想在不同的范围内使用this 时,您通常需要获取对this 的引用(setTimeout 就是一个很好的例子)。

    var MyClass = function() {
       setTimeout(function() { this.myMethod(); },100);
    };

    MyClass.prototype.myMethod = function() {
      console.log('hi there');
    }

    var myObject = new MyClass();

在上面的代码中,你会得到一个错误,因为当 setTimeout 函数被执行时,它是在 this === window 的全局范围内执行的,而你在 window 对象上没有名为 myMethod() 的函数。

要纠正这个问题,你可以这样做:

    var MyClass = function() {
       var self = this;
       setTimeout(function() { self.myMethod(); },100);
    };

    MyClass.prototype.myMethod = function() {
      console.log('hi there');
    }

    var myObject = new MyClass();

即使您的 setTimeout 函数在全局范围内执行,变量 self 实际指向 MyClass(或 this)的实例,因为您执行了 self = this(并且因为 JavaScript 是 lexically scoped

另外,这只是个人喜好,你会经常看到:

var self = this;

而不是

var _this = this;

没什么大不了的,但这只是我认为值得一提的约定。

【讨论】:

  • var me = this :)
  • 人们使用var self = this;var me = this 是因为他们习惯了其他语言,实际上使用meself 作为关键字而不是this?还是有其他原因?
  • @nhgrif 假设 functionA 与一些 this 一起运行。该函数定义了一些需要使用来自functionAthisfunctionB。但是如果functionB 使用this 关键字,它将引用functionB自己的 this。我们通过将functionAthis 分配给另一个标识符来解决这个问题,该标识符可从functionB 获得。见Understanding Javascript scope with “var that = this”
  • @nhgrif 这基本上是因为你不能限定this,如果你愿意,你必须将引用保留为不同的变量。即函数A使用this,在函数A内部定义了另一个函数BA 中的 this 不一定与 B 中的 this 相同。解决方案:将this 赋值给A 中的一个变量。 编辑你打败了我
  • 我主要是在询问命名约定,但谢谢。 ;)
【解决方案2】:

对于第一点,无论如何它都会在 RAM 中,我不能说为什么用它来引用它会改变任何东西。

至于第二点,最好将对象 this 引用存储在变量中,例如 self 或 _this ,因为在 Javascript 中,当您调用函数时,函数的上下文可以通过 call 和 apply 函数来操作,这将然后改变这意味着什么。

这样:

MyClass = function(){ 
    this.name = "Petter";
    this.age = 12;
    this.toString = function(){
        return "Name: "+this.name + " Age: " + this.age;  
    }
}
myObject = new MyClass();
myObject.call({name: 'bob', age: '15'});

myObject.call 行实际上会更改 toString 中对 this 的引用,因此它将返回“姓名:鲍勃年龄:15”而不是“姓名:彼得年龄:12”

【讨论】:

  • Gabriel,你确实抓住了我的问题,因为这是一个有趣的参考。我知道如何使用它,但我不清楚这个问题:对象的操作。今天我学到了一些新东西!
【解决方案3】:
  1. 不,如果您在实例化对象时不使用 the new keyword,您可能会混淆与每个对象实例关联的原型函数。

  2. 正如亚当的回答中提到的,您只是通过添加另一个变量来使用更多内存。按照惯例,当它们是私有实例变量时,您通常会看到 variable names prefixed with an underscore

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多