Javascript 'this' value changing, but can't figure out why

后端 未结 6 1920
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 08:52

I\'m a total Javascript newb, and I\'m trying to wrap my head around OLN. What I\'m encountering is that, when calling an object method from another method on the same objec

6条回答
  •  感动是毒
    2021-01-14 09:19

    You can control the execution context of the method call by using call():

    var generator = {
      generateForLevelSkillAndCount : function(level, skill, count) {
        return this['generate_' + level + '_' + skill].call(this, count);
      },
      generate_0_4 : function(count) {
        return this.generate_generic_dots.call(this, count, 3);
      },
      generate_generic_dots : function(count, maxDots) {
        /* do cool stuff and return it */
      }
    };
    

提交回复
热议问题