Javascript - 使用本地变量还是这个?

发布于 2024-12-21 19:34:48 字数 463 浏览 1 评论 0原文

我正在使用原型方法,这是场景

function Foo () {
    this.x = 5;
    this.y = 2;
    this.z = this.addValues();
}
Foo.prototype = {
    addValues:  function (){
        return this.x + this.y; 
    }
}

显然这只是一个简单的例子;在实际项目中,“addValue”函数中会有很多活动。是否可以使用“this”关键字 100 次,或者将其缓存到本地变量有助于提高性能。例如,下面的内容会有什么不同吗?

Foo.prototype = {
    addValues:  function (){
        var self = this;
        return self.x + self.y; 
    }
}

I am working with prototype methods and here is the scenerio

function Foo () {
    this.x = 5;
    this.y = 2;
    this.z = this.addValues();
}
Foo.prototype = {
    addValues:  function (){
        return this.x + this.y; 
    }
}

Obviously this is just a simple example; in real project, there will be lot of activities in the 'addValue' function. Is it fine to use 'this' keyword 100s of times or caching this to local variable helps any performance improvements. For example, the below will make any difference?

Foo.prototype = {
    addValues:  function (){
        var self = this;
        return self.x + self.y; 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏尔 2024-12-28 19:34:48

self.xthis.x 之间可能没有任何有意义的区别。 可能会有所不同的是,

  var x = this.x, y = this.y;

  // massive amounts of computation involving x and y

除非您真正参与了一些尖端游戏开发或其他工作,否则这种微观优化可能不值得。首先让你的算法数据结构达到标准,然后再担心像这样的事情。你永远不知道 JavaScript 运行时系统的开发人员何时会引入新的优化。它们无法修复错误的算法,但可以极大地影响微观优化。

There's probably no meaningful difference between self.x and this.x. What might make a difference is

  var x = this.x, y = this.y;

  // massive amounts of computation involving x and y

Such micro-optimizations are probably not worth it unless you're really involved in some cutting-edge game development or something. Get your algorithms and data structures up to snuff first, and then worry about stuff like this last. You never know when the developers of the JavaScript runtime systems will introduce new optimizations. They can't fix your bad algorithms, but they can dramatically affect micro-optimizations.

温柔戏命师 2024-12-28 19:34:48

this 是访问 xy 的标准方法。将 this 缓存到本地变量不会带来任何改进——如果有的话,首先声明 self 是在浪费空间。

您会看到的唯一可能风险是这样的:

var f = new foo();
var stupid = f.addValues;
stupid(); // whoops - this is suddenly the global object, 
          // and your method is broken.

话虽如此,我认为您不对人们滥用您的功能负责,而且我不会担心这一点。

另外,按照惯例,用作构造函数的函数应以大写字母开头。考虑将 foo 重命名为 Foo

this is the standard way to access x and y. You'll get no improvements from caching this to a local var—if anything, you're wasting space by declaring self in the first place.

The only possible risk you'd see is with something like this:

var f = new foo();
var stupid = f.addValues;
stupid(); // whoops - this is suddenly the global object, 
          // and your method is broken.

Having said that, I don't think you're responsible for people misusing your function, and I wouldn't worry about it.

Also, by convention, functions meant to be used as constructors should start with a capital letter. Consider renaming foo to Foo

喜爱皱眉﹌ 2024-12-28 19:34:48

在此示例中,将 this 分配给 self 应该不会产生任何影响。 JavaScript 不会按值分配对象,而是按引用分配对象,这意味着 self 和 this 都指向同一个对象。这样做你什么也得不到。

foo.prototype = {
    addValue:  function (){
       // self and this point to the same object! 
       var self = this;
        return self.x + self.y; 
    }
}

In this example, assigning this to self should not make any difference. JavaScript does not assign objects by value, but rather by reference, meaning that self and this both then point to the same object. You gain nothing by doing so.

foo.prototype = {
    addValue:  function (){
       // self and this point to the same object! 
       var self = this;
        return self.x + self.y; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文