javascript - 未捕获的 TypeError : *. watch 不是一个函数(FireFox 除外)

标签 javascript jquery

我知道有数千个问题与此相关,但没有人提供帮助。

问题出在 watch 事件中,它在 FireFox 中工作正常,但在其他浏览器中不起作用。

我在控制台中收到此错误:

Uncaught TypeError: Step.watch is not a function

我需要从中了解什么? watch 只能在 FireFox 中使用吗?

function Step()
{
  this.done = false;
  this.active = false;
};
function StepsControl()
{
  this.steps = new Array();
  this.addListener = function(Step,stepId)
  {
    var self = this;
    Step.watch('done', function (id_, oldval, newval)
    {
      console.log('Step[' + stepId + '].' + id_ + ' changed from ' + oldval + ' to ' + newval);
      if (self.steps[++stepId] !== undefined)
      {
        self.steps[stepId].active = true;
        console.log('Step[' + stepId + '] actived');
      }
      
      return newval;
    });
  };
  this.addStep = function(Step)
  {
    this.steps.push(Step);
    this.addListener(Step,this.steps.length-1);
  };
};

var step1 = new Step();
var step2 = new Step();
var stepsControl = new StepsControl();
stepsControl.addStep(step1);
stepsControl.addStep(step2);
step1.active = true;
step1.done = true;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

最佳答案

来自 Mozilla 文档:

Warning: Generally you should avoid using watch() and unwatch() when possible. These two methods are implemented only in Gecko, and they're intended primarily for debugging use. In addition, using watchpoints has a serious negative impact on performance, which is especially true when used on global objects, such as window. You can usually use setters and getters or proxies instead. See Browser compatibility for details. Also, do not confuse Object.watch with Object.observe.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch

关于javascript - 未捕获的 TypeError : *. watch 不是一个函数(FireFox 除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34701702/

相关文章:

javascript - 如何维护 JSON 中的 <br/> 标签?

JavaScript:如果变量更改则分配变量

javascript - 页面加载时显示与单选按钮匹配的文本字段

javascript - 通过添加 html 标签重新格式化文本

JavaScript Moodle

javascript - Semantic-UI-React:具有 2 列的卡片标题

Javascript 嵌套数组和过滤器

jQuery 移动 + Phonegap : Ajax calls not working on Android Emulator

javascript - 执行 $.find 时出现错误 "undefined is not a funcion"

javascript - 通过内部跨度的内部文本查找 li 元素