ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**JS类实现代码** ~~~ var student = { name:"名字", age:"年龄", sex:"性别", number:"学号" } function student(name,age) { this.name = name; this.age = age; } student.prototype.sex="男"; student.prototype.number = function(){ console.log("20170101"); } var stu1 = new student("小马",21); var stu2 = new student("小明",30); console.log(stu1.sex); stu2.number(); console.log(stu1.name); ~~~ **错误实例** ~~~ function student(name,age) { this.nameS = name; this.age = age; this.number = function(){ return getName(); } function getName(){ return this.nameS; } function getAge(){ return this.age; } } student.prototype.sex="男"; student.prototype.number = function(){ console.log("20170101"); } var stu1 = new student("小马",21); var stu2 = new student("小明",30); console.log(stu1.number()); ~~~