• <bdo id='lCqxW'></bdo><ul id='lCqxW'></ul>
      <legend id='lCqxW'><style id='lCqxW'><dir id='lCqxW'><q id='lCqxW'></q></dir></style></legend>

      <small id='lCqxW'></small><noframes id='lCqxW'>

      <i id='lCqxW'><tr id='lCqxW'><dt id='lCqxW'><q id='lCqxW'><span id='lCqxW'><b id='lCqxW'><form id='lCqxW'><ins id='lCqxW'></ins><ul id='lCqxW'></ul><sub id='lCqxW'></sub></form><legend id='lCqxW'></legend><bdo id='lCqxW'><pre id='lCqxW'><center id='lCqxW'></center></pre></bdo></b><th id='lCqxW'></th></span></q></dt></tr></i><div id='lCqxW'><tfoot id='lCqxW'></tfoot><dl id='lCqxW'><fieldset id='lCqxW'></fieldset></dl></div>

        <tfoot id='lCqxW'></tfoot>
      1. JavaScript setInterval 没有正确绑定到正确的闭包

        JavaScript setInterval not being properly bound to correct closure(JavaScript setInterval 没有正确绑定到正确的闭包)
        <i id='9yRR3'><tr id='9yRR3'><dt id='9yRR3'><q id='9yRR3'><span id='9yRR3'><b id='9yRR3'><form id='9yRR3'><ins id='9yRR3'></ins><ul id='9yRR3'></ul><sub id='9yRR3'></sub></form><legend id='9yRR3'></legend><bdo id='9yRR3'><pre id='9yRR3'><center id='9yRR3'></center></pre></bdo></b><th id='9yRR3'></th></span></q></dt></tr></i><div id='9yRR3'><tfoot id='9yRR3'></tfoot><dl id='9yRR3'><fieldset id='9yRR3'></fieldset></dl></div>
      2. <legend id='9yRR3'><style id='9yRR3'><dir id='9yRR3'><q id='9yRR3'></q></dir></style></legend>

                <bdo id='9yRR3'></bdo><ul id='9yRR3'></ul>

                  <small id='9yRR3'></small><noframes id='9yRR3'>

                    <tbody id='9yRR3'></tbody>

                  <tfoot id='9yRR3'></tfoot>
                • 本文介绍了JavaScript setInterval 没有正确绑定到正确的闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  大家好,我是 JavaScript 的新手,我来自 Python 和 Java 非常面向对象的世界,这是我的免责声明.

                  Hi people, I'm reasonably new to JavaScript and I come from the very object-oriented world of Python and Java, that's my disclaimer.

                  下面有两块代码,替代实现,一个在 JavaScript 中,一个在 Coffeescript 中.我正在尝试在 Meteor.js 应用程序的服务器上运行它们.我遇到的问题是当使用绑定方法this.printSomething"作为我的回调调用函数setInterval"时,一旦执行该回调,它就会失去实例的范围,导致this.bar"未定义!谁能向我解释为什么 JavaScript 或 coffescript 代码不起作用?

                  There are two chunks of code below, alternative implementations, one in JavaScript, one in Coffeescript. I am trying to run them on the server in a Meteor.js application. The problem I am experiencing is when calling the function "setInterval" using the bound-method "this.printSomething" as my callback, once that callback is executed, it loses scope with the instance resulting in "this.bar" being undefined! Can anyone explain to me why either the JavaScript or the coffescript code isn't working?

                  function Foo(bar) {
                    this.bar = bar;
                  
                    this.start = function () {
                      setInterval(this.printSomething, 3000);
                    }
                  
                    this.printSomething = function() {
                      console.log(this.bar);
                    }
                  }
                  
                  f = new Foo(5);
                  f.start();
                  

                  咖啡脚本实现

                  class foo
                      constructor: (bar) ->
                          @bar = bar
                  
                      start: () ->
                          Meteor.setInterval(@printSomething, 3000)
                  
                      printSomething: () ->
                          console.log @bar
                  
                  x = new foo 0
                  x.start()
                  

                  推荐答案

                  您在 setInterval 回调中丢失了 Foo 的上下文.您可以使用 Function.bind 来将上下文设置为类似这样以将回调函数引用的上下文设置回 Foo 实例.

                  You lose your context of Foo in the setInterval callback. You can use Function.bind to set the context to something like this to set the context for the callback function reference back to Foo instance.

                  setInterval(this.printSomething.bind(this), 3000);
                  

                  随叫随到

                  setInterval(this.printSomething, 3000);
                  

                  回调方法获取全局上下文(在 web 的情况下为窗口或在节点等租户的情况下为全局),因此您不会从 this 那里获得属性 bar指的是全局上下文.

                  The callback method gets the global context (window in case of web or global in case of tenants like node) so you don't get property bar there since this refers to the global context.

                  小提琴

                  或者只是

                   this.printSomething = function() {
                       console.log(bar); //you can access bar here since it is not bound to the instance of Foo
                    }
                  

                  这篇关于JavaScript setInterval 没有正确绑定到正确的闭包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                  <small id='s0ADX'></small><noframes id='s0ADX'>

                  <legend id='s0ADX'><style id='s0ADX'><dir id='s0ADX'><q id='s0ADX'></q></dir></style></legend>
                  • <bdo id='s0ADX'></bdo><ul id='s0ADX'></ul>
                    <i id='s0ADX'><tr id='s0ADX'><dt id='s0ADX'><q id='s0ADX'><span id='s0ADX'><b id='s0ADX'><form id='s0ADX'><ins id='s0ADX'></ins><ul id='s0ADX'></ul><sub id='s0ADX'></sub></form><legend id='s0ADX'></legend><bdo id='s0ADX'><pre id='s0ADX'><center id='s0ADX'></center></pre></bdo></b><th id='s0ADX'></th></span></q></dt></tr></i><div id='s0ADX'><tfoot id='s0ADX'></tfoot><dl id='s0ADX'><fieldset id='s0ADX'></fieldset></dl></div>
                        <tfoot id='s0ADX'></tfoot>

                              <tbody id='s0ADX'></tbody>