<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body></body>
</html>
<script>
// class关键字 让我们在书写的时候更加清晰(还是语法糖)
// function Person(name, age) {
// this.name = name;
// this.age = age;
// }
// Person.prototype = {
// constructor: Person
// };
// console.log(new Person("jack", 19));
// 定义类 class 类名
class Person {
static myStaticProp = 19; // 通过static关键字来定义静态属性 一般用于定义全局常量
constructor(name, age) {
// 构造器函数, 在new的时候, 系统会自动调用这个函数来初始化new 出来的实例对象
this.name = name;
this.age = age;
this.sayName = this.sayName.bind(123); // react的时候,事件绑定的时候
}
sayName() {
// 这些在类体内部的函数就是当前挂载到原型对象上面的实例方法
console.log("this1", this);
}
static eat() {
// 静态方法
console.log("this2", this);
}
}
let p1 = new Person("Jack", 26);
console.dir(Person);
console.log(p1);
p1.sayName();
let { sayName } = p1;
sayName();
Person.myStaticProp; // 全局的常量
Person.eat();
// 静态属性 // 通过static关键字来定义静态属性
// 静态方法 // 通过static关键字来定义静态方法
</script>
网友评论