JavaScript static/singelton-this vs _this vs object name [英] Javascript static/singelton - this vs _this vs object name

查看:102
本文介绍了JavaScript static/singelton-this vs _this vs object name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关性能和最佳实践的问题.

This is a question about performance and best practice.

假设我有一个js对象,其中封装了大量的辅助方法.该对象被视为静态类,这意味着它永远不会实例化,并且其所有方法基本上都是辅助方法. 当使用事件和jQuery时,对象的this范围会不断变化,并且由于它具有相当多的方法,我想知道什么是最佳实践-在每个方法的开头将this保存到_this或简单地使用对象名称MyObject.

Assuming I have a js object that encapsulates a large number of helper methods. The object is being treated as a static class, meaning it is never instantiated and all its methods are basically helper methods. When using events and jQuery, the object's this scope keeps changing, and since it has a fairly large number of methods I am wondering what is best practice - saving this into _this at the beginning of each method or simply use the object name MyObject.

多年来,当涉及到辛格尔顿/静态物体时,我一直都这样做,但是我认为必须只有一种最佳实践,现在是时候采用最佳方法了.

Over the years I've been doing both, when it comes to singelton/static objects, but I figured there must be just 1 best practice and it's about time to adopt the best approach.

我目前的研究表明,使用_this而不是直接调用MyObject带来的好处主要是以下两个方面:

My current research shows that the benefits that come with using _this instead of directly calling MyObject are mainly these two:

  • 如果对象名称更改,则_this将始终有效
  • 由于浏览器停留在相同的范围内,不需要每次都找到MyObject的范围,因此速度可能更快(尽管尚未看到性能测试结果).
  • if the object's name changes, _this will always work
  • May be faster (although haven't seen performance testing results) since the browser stays in the same scope and does not need to find out the scope of MyObject every time.

使用MyObject的优点:

  • 减少编写代码.
  • 垃圾收集管理? (分配的变量较少)
  • 对于某些开发人员(适用多个this),可能更具可读性
  • 由于MyObject始终有效,因此重构代码更容易
  • Less code to write.
  • Garbage collection management? (less variables to assign)
  • May be more readable for some developers (where multiple this apply)
  • Easier to refactor code since MyObject will always work

我想知道是否有一种全局保存_this的方法(当然仅在对象的作用域之内),并且避免在每个方法的开头分配它.如果不是,那么我是否缺少其他优点/缺点,或者直接调用对象名称被认为是不好的做法.

I would like to know if there is a way of globally saving _this (only inside the object's scope of course) and avoid assigning it at the beginning of each method. If not - are there other pros/cons that I am missing or is it considered bad practice to call the object name directly.

这是一个供参考的简化对象(实际对象有更多方法).

This is a simplified object for reference (real object has many more methods).

    var MyObject = {
       init: function() {

         this.registerEvents();
         //other stuff

       },
       registerEvents: function() {

         this.registerOnSomeEvent();
         //registering more events..
         //...

       },

       registerOnSomeEvent: function() {
          var _this = this;
          jQuery('#someid').click(function() {
             _this.doSomething();//option 1
             MyObject.doSomething();//option 2
          });
       },

       doSomething: function() {
         //doing something...
       }
    }

MyObject.init();

谢谢您的帮助!

推荐答案

您可以将整个对象封装在一个闭包中以实现此目的,而无需在每个函数上都指定_this:

You can encapsulate the entire object in a closure to achieve this without specifiying _this on every function:

window.MyObject = (function () {
    var self = {
        init: function() {

            self.registerEvents();
            //other stuff

        },
        registerEvents: function() {

            self.registerOnSomeEvent();
            //registering more events..
            //...

        },

        registerOnSomeEvent: function() {
            jQuery('#someid').click(function() {
                self.doSomething();//option 1
                self.doSomething();//option 2
            });
        },

        doSomething: function() {
            //doing something...
        }
    };

    self.init();

    return self;
})();

这篇关于JavaScript static/singelton-this vs _this vs object name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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