<!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>ES6类class与面向对象</title> </head> <body> <script> class Test{ constructor(name,age){//新建父类 this.name = name; this.age = age; } static getClassName(){//静态方法 return 'Test'; } setName(name){//原型方法 this.name = name; } setAge(age){//原型方法 this.age = age; } get info(){//获取属性 return this.name + this.age; } get job(){//获取属性 return '前端工程师'; } } var t1 = new Test('Test',123); console.log(t1);//Test {name: "Test", age: 123} class NextTest extends Test{//子类继承父类所有属性和方法 constructor(name,age,num){ super(name,age); this.num = num; } setNum(num){ this.num = num; } } var n1 = new NextTest('NextText',111,123456789); console.log(n1);//NextTest {name: "NextText", age: 111, num: 123456789} var todo = new class Todo{//新建立即执行类 constructor(name){ this.name = name; } }('todo'); console.log(todo);//Todo {name: "todo"} var notFound = new NotFound();//类不会自动提升,这样调用会报错 NotFound is not defined class NotFount{ constructor(name){ this.name = name; } } </script> </body> </html>
ES6基础知识(ES6类class与面向对象)
猜你喜欢
转载自www.cnblogs.com/xingxingclassroom/p/10387080.html
今日推荐
周排行