es6——新增的数据类型 class

版权声明:转发博客 请注明出处 否则必究 https://blog.csdn.net/lucky541788/article/details/82632243

es5的面向对象方法

    {
        //构造函数
        function Person1(name,age){
            this.name=name;
            this.age=age;
        }

        Person1.prototype={
            constructor:Person1,
            print(){
                console.log('my name is ' + this.name + ', ' + this.age + ' years old.');
            }
        };

        let person=new Person1('bob',20);

        console.log(person);//Person {name: "bob", age: 20}
        person.print();//my name is bob, 20 years old.
    }

es6的class面向对象

    {
        class Person2{
            constructor(name,age){
                this.name=name;
                this.age=age;
            }

            print(){
                console.log('my name is ' + this.name + ', ' + this.age + ' years old.');
            }
        }

        let person=new Person2('bob',20);

        console.log(person);//Person {name: "bob", age: 20}
        person.print();//my name is bob, 20 years old.
    }

猜你喜欢

转载自blog.csdn.net/lucky541788/article/details/82632243