Loading

ES6私有属性、存取器

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>ES6私有属性、存取器</title>
</head>
<body>
	<script >
			class Person {
	        name
	        #web //私有属性是指仅在类内部可访问和操作的属性, 外部无法直接访问和修改

	        constructor(name, web) {
	            this.name = name
	            this.#web = web
	        }

	        //使用存取器 getter 获取私有属性
	        get web() {
	            return this.#web
	        }

	        //使用存取器 setter 设置私有属性
	        set web(value) {
	            this.#web = value
	        }

	        info() {
	            return `姓名:${this.name} 个人网站:${this.web}`
	        }
	    }

	    let person = new Person("邓瑞", "xxx.com")

	    console.log("person", person)
	    console.log("web", person.web) //使用存取器 getter 获取私有属性
	    console.log("info", person.info())

	    person.web = "www.abc.com" //使用存取器 setter 设置私有属性
	    console.log("web", person.web)
	</script>
</body>
</html>
posted @ 2024-04-15 16:09  Devinwon  阅读(1)  评论(0编辑  收藏  举报