From 0b83652ee65e95066990f2008540af8bfbee2f68 Mon Sep 17 00:00:00 2001 From: yihan12 <44987698+yihan12@users.noreply.github.com> Date: Thu, 14 Dec 2023 11:00:12 +0800 Subject: [PATCH] Update Object.md --- javascript/Data Types/Object.md | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/javascript/Data Types/Object.md b/javascript/Data Types/Object.md index 5f07c12..1e1fdcf 100644 --- a/javascript/Data Types/Object.md +++ b/javascript/Data Types/Object.md @@ -72,6 +72,7 @@ me.printIntroduction(); // My name is Mukul. Am I studying?: true ``` #### 使用ES6 Class +```javascript class Vehicle { constructor(name, maker, engine) { this.name = name; @@ -149,3 +150,43 @@ for (let key in person1) { } console.log(person1, person) // person1的原型上会继承person的gender属性 ``` + +### 对象的setter和getter + +```javascript +const person = { + firstName: "John", + lastName: "Doe", + fullName: function() { + return this.firstName + " " + this.lastName; + } +}; + +console.log(person.fullName()) // 'John Doe' +const person2 = { + firstName: "John", + lastName: "Doe", + get fullName() { + return this.firstName + " " + this.lastName; + } +}; + +console.log(person2.fullName) // 'John Doe' +``` + +```javascript +const person = { + firstName: "John", + lastName: "Doe", + language: "", + set lang(lang) { + this.language = lang; + } +}; + +person.lang = "en"; + +console.log(person.language) // 'en' +``` + +