javascript - this.init 不是函数
问题描述
我正在为班级做这个,他们的测试说我的构造函数没有被初始化,所以我查了一下发现this.init()
。现在我得到“this.init() 不是一个函数,我不知道为什么。
我的代码:
function CuboidMaker(length, width, height){
const volume = () => {
return this.length * this.width * this.height
}
const surfaceArea = () => {
return 2 * (this.length * this.width + this.length * this.height + this.width * this.height)
}
this.length = length
this.width = width
this.height = height
this.volume = volume
this.surfaceArea = surfaceArea
this.init()
}
解决方案
仅仅是您没有创建构造函数的新实例吗?
此外,volume
andsurfaceArea
都是函数,需要调用。
function CuboidMaker(length, width, height){
const volume = () => {
return this.length * this.width * this.height;
}
const surfaceArea = () => {
return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
}
this.length = length;
this.width = width;
this.height = height;
this.volume = volume();
this.surfaceArea = surfaceArea();
}
const a = new CuboidMaker(1, 2, 3);
console.log(a);
推荐阅读
- codeigniter-3 - 从 Windows 中的 phpdesktop 应用程序发送的邮件我必须参考这个项目网站:http://github.com/cztomczak/phpdesktop
- java - 我正在创建一个简单的 JDBC Java 应用程序,找不到找不到类的异常
- selenium-chromedriver - 从python脚本生成可执行文件不起作用
- reactjs - url更改时反应路由器v4 history.push()不起作用
- reactjs - 如何使用默认的提交验证将来自反应路由器 dom 的链接添加到提交按钮
- android - 如何构建快速反应的溢出下拉菜单?
- mysql - 带有空列的 MYSQL INNER JOIN
- ajax - Ajax 在 ASP.NET mvc 中返回“未找到”
- java - RecyclerView Android 中的微调器
- javascript - 如何获取json对象的长度?