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();
原文地址:https://www.cnblogs.com/wt7018/p/11535892.html