(几乎)在闭包中始终有Polymer定义? [英] (almost) always have Polymer definition inside a closure?

查看:75
本文介绍了(几乎)在闭包中始终有Polymer定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于复杂的元素,(几乎)总是在封闭符中始终具有Polymer定义以保持所有仅应在内部修改的变量和方法(而不是将其附加到元素上)相对是一种好习惯(例如,'this ')?

For complicated elements, is is a good practice to (almost) always have Polymer definition inside a closure to keep all the variables and methods which should only be modified internally private, as opposed to attaching them to the element (e.g. 'this')?

喜欢以下内容:

<polymer-element name="animating-element">
   <script>


    (function() { 

       var privateObj = {};

       privateObj.internalState = 0; 

       //private static method
       privateObject.setupState = function(polymerObject) {
          if(polymerObject.stateExposedToOutside == /* some conditions */) { 
             privateObject.internalState = 1;
          }

       }

       Polymer('animating-element', {
          stateExposedToOutside: 0,
          ready: function() {

              privateObj.setupState(this);
              this.animate();

          },
          animate: function() {

          }
      });
    })();

   </script>

</polymer-element>

推荐答案

否! 上面的代码存在一个主要问题.由于privateObj由闭包密封,因此它是静态的(例如,在该组件的所有实例之间共享),并且无法保持每个实例的状态.

No! There is one major problem with above code. Since privateObj is sealed by the closure, it is static (e.g. shared among all the instances of that component) and cannot keep the state for each of them.

此外,我们的想法是每个组件的状态仅由其属性决定,因此应避免保留内部状态对象.

Moreover the idea is that the state of each component solely be determined by its attributes so keeping an internal state object should be avoided.

这篇关于(几乎)在闭包中始终有Polymer定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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