在我的 Javascript 代码中,未定义 Stats

问题描述

var count_particles,stats,update;
stats = new Stats();
stats.setMode(0);
stats.domElement.style.position = "absolute";
stats.domElement.style.left = "0px";
stats.domElement.style.top = "0px";
document.body.appendChild(stats.domElement);
count_particles = document.querySelector(".js-count-particles");
update = function () {
  stats.begin();
  stats.end();
  if (window.pJSDom[0].pJS.particles && window.pJSDom[0].pJS.particles.array) {
    count_particles.innerText = window.pJSDom[0].pJS.particles.array.length;
  }
  requestAnimationFrame(update);
};
requestAnimationFrame(update);

它说 未捕获的 ReferenceError:未定义统计数据 请问有人可以帮忙吗?

解决方法

错误不是标题中的 stats is not defined - 它是 Stats is not defined。请注意大写字母 - 您缺少 Stats 类的定义。

,

根据您的代码,stats 已定义,但 Stats 未定义。你的标题有错别字。

Stats 应该是一个从未定义的类。这是你如何在 Javascript 中创建一个类:

class ClassName { constructor() { ... } }

在您的情况下,类名是 Stats,构造函数是类中的方法,例如代码中的 setMode()

有关详细信息,请访问 Javascript Classes at Mozilla

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...