// 示例代码:JavaScript 对象属性
// 创建一个对象
let person = {
firstName: "John",
lastName: "Doe",
age: 25,
get fullName() {
return this.firstName + " " + this.lastName;
},
set fullName(name) {
let parts = name.split(" ");
this.firstName = parts[0];
this.lastName = parts[1];
}
};
// 访问属性
console.log(person.firstName); // 输出: John
console.log(person.fullName); // 输出: John Doe
// 修改属性
person.age = 26;
person.fullName = "Jane Smith";
console.log(person.firstName); // 输出: Jane
console.log(person.lastName); // 输出: Smith
console.log(person.age); // 输出: 26
person 的对象,包含多个属性如 firstName、lastName 和 age。get 关键字定义了一个只读属性 fullName,它返回 firstName 和 lastName 的组合。set 关键字定义了一个 setter 方法,允许通过 fullName 属性设置 firstName 和 lastName。上一篇:js 字符串比较大小
下一篇:js base64编码
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站