<script type="text/javascript">
/*
class 在es6中用于定义类
*/
//es5写法
function Person(name,age){
this.name = name,
this.age = age
}
Person.prototype = {
sayName(){alert(this.name)},
sayAge(){alert(this.age)}
}
const obj = new Person("fy",18);
//es6写法
class Person2{ //类名叫做Person2
//私有属性 写在constructor中
constructor(name,age){
this.name = name,
this.age = age
}
//constructor之外的就是共有方法
sayName(){alert(this.name)}//可以不用写任何符号
sayAge(){alert(this.age)}
}
const obj2 = new Person2("kobo",20)
obj.sayName();//fy
obj2.sayName();//kobo
</script>
网友评论