JS定义类

// ES6
class Person{
    constructor(name, age){
        this.name = name;
        this.age = age;
    }
    getName(){
        console.log(this.name)
    }
}
p = new Person('alex', 34);
p.getName();


// ES5
function Person(name){
        this.name = name;
        this.getName = function(){
        console.log(this.name)
    }
}
var p = new Person('tom');
p.getName();


//ES5
var person = {
    name: 'tom',
    getName: function(){
        console.log(this.name);
    }
}
person.getName();

 

posted @ 2019-09-17 18:02  市丸银  阅读(228)  评论(0编辑  收藏  举报