Class

最后更新于:2022-04-02 03:27:35

[TOC] ## 示例 ## hello world old: ``` function Student(name) { this.name = name; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); } ``` new: ``` export class Student { constructor(name) { this.name = name; } hello() { alert('Hello, ' + this.name + '!'); } } ``` 调用方式都是一样 ``` // 导入 import {Student} from "student.js" var student = new Student("word"); student.hello() ``` ### 传入对象 ``` export class person{ constructor({name,age}) { this.name=name this.age=age } echo(){ return this.name+this.age } } ``` 调用 ``` import {person} from "person.js"; let good1 =new person({name:"cpj",age:123}); ```
';