<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/atom-one-dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<pre>
<code class="hljs">
{
//实用之处①:限制访问(get)与修改(set)
let user = "visitor";
let person = {
firstName: "Hello",
lastName: "World",
get fullName() {
if(user === "visitor") {
alert("Access Denied!");
return ;
}
return this.firstName + " " + this.lastName;
},
set fullName(value) {
if(value.length < 6) {
alert("The value is too short!");
return;
}
[this.firstName, this.lastName] = value.split(" ");
}
}
console.log("------------------------ 6 ------------------------");
console.log(person.fullName);//alert("Access Denied!");
user = "admin";
console.log(person.fullName);//Hello World
person.fullName = "123";
console.log(person.fullName);//alert("The value is too short!");
person.fullName = "Captain America";
console.log(person.fullName);//Captain America
}
</code>
</pre>
</body>
</html>