js是一个基于面向对象设计的单线程的静态脚本语言 本质没有继承 类 等等语法
// 声明类
class Person {
}
var p1 = new Person()
console.log(p1);
// 匿名类
var fn = class {
}
var f1 = new fn();
console.log(f1);
js没有类 是原型的思想设计的类
class Person {
constructor(a, b, c) {
console.log(666); // 666
this.x = a;
this.b = b;
}
// 对象的属性
life = 50;
// 对象的原型对象的方法
say() {
console.log(111, this.x); // 111 10
}
// 静态属性
static life = 200;
// 静态方法
static say2() {
console.log(222);
}
};
var p1 = new Person(10, 20, 30);
// Person类的内部可以写一个constructor方法 用这个类创建对象时 就会运行构造函数
// 不写构造函数 系统会默认有一个空的构造运行
console.log(p1); // {life: 50, x: 10, b: 20}
p1.say();
console.log(Person.life); // 200
console.log(Person.say2); // say2() {console.log(222);}
继承
class Person {
life = 1;
constructor(name) {
this.name = name
this.makeMoney = function () {
console.log(this.name + "赚钱");
}
}
run() {
console.log(this.name + "跑步");
}
static URL = "www"
}
var p1 = new Person("karen");
var p2 = new Person("jack");
console.log(p1, p2, Person.URL); // {life: 1, name: 'karen', makeMoney: ƒ} {life: 1, name: 'jack', makeMoney: ƒ} 'www'
class Student extends Person {
constructor(name) {
super(name) // 让Person类的构造函数的逻辑去帮我们构建对象空间
this.age = 20
this.book = "书名"
}
}
var s1 = new Student("marry");
console.log(s1, Student.URL); // {life: 1, name: 'marry', age: 20, book: '书名', makeMoney: ƒ} 'www'