ES6——面向对象-基础
2024-09-05 10:27:28
面向对象原来写法
类和构造函数一样
属性和方法分开写的
// 老版本
function User(name, pass) {
this.name = name
this.pass = pass
} User.prototype.showName = function () {
console.log(this.name)
}
User.prototype.showPass = function () {
console.log(this.pass)
} var u1 = new User('able', '1233')
u1.showName()
u1.showPass()
// 老版本继承
function VipUser(name, pass, level) {
User.call(this, name, pass)
this.level = level
}
VipUser.prototype = new User()
VipUser.prototype.constructor = VipUser
VipUser.prototype.showLevel = function () {
console.log(this.level)
} var v1 = new VipUser('blue', '1234', 3)
v1.showName()
v1.showLevel()
新版面向对象
有了 class 关键字、构造器
class 里面直接加方法
继承,super 超类==父类
class User {
constructor(name, pass) {
this.name = name
this.pass = pass
} showName() {
console.log(this.name)
}
showPass() {
console.log(this.pass)
}
} var u1 = new User('able2', '111')
u1.showName();// able2u1.showPass(); // 111 // 新版本继承
class VipUser extends User {
constructor(name, pass, level) {
super(name, pass)
this.level = level
}
showLevel(){
console.log(this.level)
}
}
v1 = new VipUser('blue', '123', 3)
v1.showLevel();// 3
最新文章
- c++ 类的静态变量
- 编写jquery常用插件的基本格式
- 使用Myeclipse创建自定义签名debug keystore
- 记录一些容易忘记的属性 -- UIImageView
- 中国海洋大学第四届朗讯杯高级组 I Cuckoo for Hashing
- Qt5遇到的问题
- ViewRootImpl和WindowManagerService笔记
- selenium+firefox时每次都要导入数据解决方法解决方法:
- ATM取款小项目
- webstorm快捷键收集【转发】
- PostgreSQL索引描述
- C#-----线程安全的ConcurrentQueue<;T>;队列
- java求最大值以及定义方法调用
- 一个RTSP/RTP over TCP 的丢包引起的问题
- LeetCode(51):N皇后
- javascript es6 箭头函数
- 求1~n整数中1出现的次数(《剑指offer》面试题43)
- 基于boot2docker部署Docker环境
- Python进行Android开发步骤
- php中ob缓存机制
热门文章
- mysql FULL JOIN关键字 语法
- sass、less中的scoped属性
- BZOJ 2288: 【POJ Challenge】生日礼物 贪心 + 堆 + 链表
- matplotlib中中文字体配置
- tomcat安全配置参考
- java 中创建线程有哪几种方式?
- fedora23安装php,mysql
- Delphi XE2 之 FireMonkey 入门(21) - 和 FMX 相关的类(表)
- debian 配置静态ip
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_3_BufferedInputStream_字节缓冲