我知道有数千个问题与此相关,但没有人提供帮助。
问题出在 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/