这个怎么处理,在网页环境下运行出错
VM2027:1 Uncaught SyntaxError: Identifier 'age' has already been declared
at <anonymous>:1:1
at n.runCode (code.js?v=202012151901:1)
at Object.success (code.js?v=202012151901:1)
at j (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at x (jquery.min.js:5)
at XMLHttpRequest.b (jquery.min.js:5)
源码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="cYear"></div>
<script>
year = null;
myAge = 2;
year = prompt("请输入出生年份:");
class age {
constructor() {
console.log('初始化年龄');
this._age = 18;
}
get getAge() {
// 获取当前年份
console.log('获取年份');
return this._age;
}
set setAge(year = 2000) {
console.log('设置年份');
let curYear = new Date().getFullYear();
// console.log(typeof parseInt(year),typeof curYear);
if (year.length < 4 || parseInt(year) > curYear) {
return this._age;
} else {
console.log(this._age = curYear - year);
return this._age;
}
}
}
let myAge = new age();
myAge.setAge = year;
cYear.innerHTML = `你的年龄为${myAge.getAge}岁`;
myAge = 2;
</script>
</body>
</html>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星