JavaScript setInterval not being properly bound to correct closure

后端 未结 3 1955
南旧
南旧 2021-01-27 02:03

Problem

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.

There are two ch

3条回答
  •  悲&欢浪女
    2021-01-27 02:45

    When you enter a function, you get a new scope in javascript. You can inherit from the parent scope, but the value of this changes. In coffeescript, you can use the fat arrow (which looks like it is going to be part of ecmascript 6) which basically retains a reference to this before going into the new scope.

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

    The standard way to handle this kind of thing in javascript is to create a reference to this at the point you want to refer to, and then use the reference in your out of scope calls...

    function Foo(bar) {
    
      // make reference to `this` at the point
      // where you want to use it from
      self = this;
    
      self.bar = bar;
    
      self.start = function () {
        setInterval(self.printSomething, 3000);
      }
    
      self.printSomething = function() {
        console.log(self.bar);
      }
    }
    
    f = new Foo(5);
    f.start();
    

提交回复
热议问题