JavaScript面向对象编程
发布日期:2021-04-30 21:05:47 浏览次数:125 分类:精选文章

本文共 1111 字,大约阅读时间需要 3 分钟。

JavaScript ??? Class ??

????

? JavaScript ?????????? Object ???????????????????????Object.prototype ???????????

var Student = {
name: "studentName",
age: 18,
run: function() {
console.log(this.name + " run...");
}
};
var xiaoming = {
name: "xiaoming"
};
// xiaoming ???? Student
xiaoming.__proto__ = Student;
xiaoming.run(); // xiaoming run...
console.log(xiaoming.age); // 18

Class ???ES6?

class ???? ES6 ?????????????????????????

// ???
class Student {
constructor(name) {
this.name = name;
}
hello() {
alert('hello');
}
}
// ???
class XiaoStudent extends Student {
constructor(name, grade) {
super(name); // ????????
this.grade = grade;
}
myGrade() {
alert("?????");
}
}
// ????
var xiaoming = new Student("xiaoming");
xiaoming.hello(); // hello
var xiaohong = new XiaoStudent("xiaohong", 80);
xiaohong.myGrade(); // ?????

???

__proto__ ??????????????????? JavaScript ??????????

console.log(xiaoming.__proto__); // ?? Student ??
console.log(Student.__proto__); // ?? Object ??

???????????????????????????????

上一篇:mybatis(分页与通过mybatis-generator工具生成java代码)
下一篇:Leetcode--424. 替换后的最长重复字符

发表评论

最新留言

很好
[***.229.124.182]2026年06月10日 10时41分13秒