ES6中增加了面向?qū)ο?br>
1、使用class關(guān)鍵字定義
2综液、構(gòu)造器使用constructor定義
案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>面向?qū)ο?lt;/title>
<script>
// ES6中面向?qū)ο?使用class關(guān)鍵字定義
// 構(gòu)造器使用constructor定義
// 定義Person類
class Person {
// 構(gòu)造器
constructor(name, age){
this.name = name
this.age = age
}
// getName
getName(){
return this.name
}
// setName
setName(name){
this.name = name
}
// getAge
getAge(){
return this.age
}
// setAge
setAge(age){
this.age = age;
}
}
// 創(chuàng)建Person實(shí)例
let person = new Person('張三', 20)
alert(person.getName());
alert(person.getAge());
person.setName('張三豐')
person.setAge(101);
alert(person.getName());
alert(person.getAge());
</script>
</head>
<body>
</body>
</html>