javascript - this.add1 不是函数
问题描述
为什么我们不能像这里一样绑定它
function UserCreator(name, score) {
this.name = name;
this.score = score;
}
UserCreator.prototype.increment = function () {
function add1() {
this.score++;
}
this.add1()
};
UserCreator.prototype.login = function () {
console.log("login");
};
const user1 = new UserCreator("Eva", 9)
user1.increment()
返回“this.add1 不是函数”。当然,我可以直接做,但有时你需要子方法,我想了解它是如何工作的
解决方案
所以本质上它正在运行window.add1()
,如果使用 add1() 调用它,它将运行window.score++
返回一个 NaN。需要研究我的“这个”概念。
推荐阅读
- batch-file - .bat 转换为 .exe 未运行
- php - 每秒 - 磁盘已满 - compute.projects.setCommonInstanceMetadata
- javascript - 在 ajax 调用运行之前为变量赋值
- regex - HTAccess 将特定的 URL 重定向到新的域页面,然后休息到 Root
- time-complexity - O(nm/8 * log(nm/8)) + O(nm/9 * log(nm/9)) + ... + O(nm/m * log(nm/m)) 等于多少?
- linq - 当 Guid 不起作用时,如何在 LINQ to SQL 中获取随机记录
- visual-studio-code - 安装完成后visual studio出现黑屏
- android - 如何检查android webview中的所有元素
- r - 我如何在文件夹中列出所有带有 .nc (netcdf) 的文件并从 10 个变量中提取 1 个变量?
- python - Python:无法从字符串转换为浮点数