JavaScript getter and setter All In One
JavaScript getter and setter All In One
getter & setter

JavaScript Object Accessors
JavaScript Accessors (Getters and Setters)
ECMAScript 5 (2009) introduced Getter and Setters.
Getters and setters allow you to define Object Accessors (Computed Properties).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
object getter and setter
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-09-05
* @modified
*
* @description object getter & setter
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link https://youtu.be/bl98dm7vJt0?t=332
* @solutions
*
*/
const log = console.log;
const person = {
name: "xgqfrms",
firstName: "web",
lastName: "fullstack",
get fullName () {
log(`\nfullName = ${this.firstName} ${this.lastName}`);
return `${this.firstName} ${this.lastName}`;
// return this.firstName + this.lastName;
},
set fullName (name) {
// const names = name.split(` `).map(item => item.trim());
// this.firstName = names[0];
// this.lastName = names[1];
[this.firstName, this.lastName] = name.split(` `).map(item => item.trim());
// [this.firstName, this.lastName, ...others] = name.split(` `).map(item => item.trim());
},
}
log(person.fullName);
person.fullName = `abc xyz`;
log(person.fullName);
/*
fullName = web fullstack
web fullstack
fullName = abc xyz
abc xyz
*/
class getter and setter
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-09-05
* @modified
*
* @description class getter & setter
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link https://youtu.be/y4wDanUBNmE?t=347
* @solutions
*
*/
const log = console.log;
class Square {
constructor (size = 0) {
// init
this.acreage = size**2;
this.width = size;
this.height = size;
this.size = size;
}
get area () {
log(`\narea = ${this.acreage}`);
return this.acreage;
}
set area (acreage = 0) {
const size = Math.sqrt(acreage);
log(`area size =`, size);
this.acreage = size**2;
this.width = size;
this.height = size;
this.size = size;
}
}
const test = new Square(3);
log(test.area);
test.area = 36;
log(test.area);
/*
area = 9
9
area size = 6
area = 36
36
*/
class static getter & setter
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-09-05
* @modified
*
* @description class static getter & setter
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
let window = {
username: "xgqfrms",
};
// let window = window || {
// username: "xgqfrms",
// };
// global variable
// global.username = "web fullstack";
let username = "web fullstack";
class Person {
constructor(name = `xgqfrms`, dollar = 100) {
this.username = name;
this.money = dollar;
}
// static property / public class field
static staticName = `static property / public class field`;
// static methods just only for the Utils function
static get getStaticName() {
log(`\nstaticName =`, Person.staticName);
return Person.staticName || Person.name;
}
static get userName() {
log(`\nstatic userName =`, window.username || global.username);
return window.username || global.username;
}
static set userName(name) {
log(`\nnew name =`, name);
if(window.username) {
window.username = name;
} else {
global.username = name;
}
}
get fortune() {
log(`\nget money =`, this.money);
return this.money;
}
set fortune(dollar) {
log(`\nset money =`, dollar);
this.money = dollar;
}
// static 只能修改全局属性,不能用于类实例中
// static get fortune() {
// log(`get money =`, this.money);
// return this.money;
// }
// static set fortune(dollar) {
// log(`set money =`, dollar);
// this.money = dollar;
// }
}
const user = new Person(`web fullstack`);
log(user.fortune);
user.fortune = 888;
log(user.fortune);
log(Person.getStaticName);
// staticName = static property / public class field
log(Person.staticName);
// static property / public class field
log(Person.userName);
// static userName = xgqfrms
Person.userName = "abc xyz";
log(Person.userName);
// static userName = abc xyz
/*
get money = 100
100
set money = 888
get money = 888
888
*/
Object.defineProperty
refs
https://javascript.info/property-accessors
https://www.hongkiat.com/blog/getters-setters-javascript/
https://www.w3schools.com/js/js_object_accessors.asp
https://stackoverflow.com/questions/812961/getters-setters-for-dummies
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
JavaScript getter and setter All In One的更多相关文章
- JavaScript getter和setter
对象的属性是由属性名name,值key,和其他特性(可读写性 writable,可枚举性enumerable,可配置性configurable)组成的.从ES5开发,提供了getter和setter ...
- [Javascript] Getter and Setter Abstractions
JavaScript provides primitive types and means of processing those. However, those are not enough. Re ...
- javascript的getter和setter(转)
显然这是一个无关IE(高级IE除外)的话题,尽管如此,有兴趣的同学还是一起来认识一下ECMAScript5标准中getter和setter的实现.在一个对象中,操作其中的属性或方法,通常运用最多的就是 ...
- JavaScript中闭包实现的私有属性的getter()和setter()方法
注意: 以下的输出都在浏览器的控制台中 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- javascript中的function命名空間與模擬getter、setter
function的命名空間 在javascript中,function也可以擁有自己的命名空間例如以下這段程式碼: 12345678 function () { return 'I am A';} A ...
- javascript权威指南笔记--javascript语言核心(五)--getter和setter属性
getter和setter属性: var p = { x:1.0, y:1.0, get r(){ return Math.sqrt(this.x*this.x + this.y * this.y); ...
- javascript中的getter和setter
在ECMAScript 5中,属性值可以用一个或两个方法代替,这两个方法就是getter和setter var man = { name : 'lidg', weibo : '@lidg', get ...
- 基于 getter 和 setter 撸一个简易的MVVM
Angular 和 Vue 在对Angular的学习中,了解到AngularJS 的两个主要缺点: 对于每一次界面时间,Ajax 或者 timeout,都会进行一个脏检查,而每一次脏检查又会在内部循环 ...
- js中的访问器属性中的getter和setter函数实现数据双向绑定
嗯,之前在读js红宝书的时候,在对象那一章有介绍属性类型.第一种数据类型指的是数据属性,第二种是访问器属性.在初识vue的时候,其双向数据绑定也是基于访问器属性中的getter和setter函数原理来 ...
随机推荐
- Scalable Go Scheduler Design Doc
https://docs.google.com/document/d/1TTj4T2JO42uD5ID9e89oa0sLKhJYD0Y_kqxDv3I3XMw/ Scalable Go Schedul ...
- hessian-serialization
http://hessian.caucho.com/doc/hessian-serialization.html Table of Contents 1. Introduction2. Desig ...
- How to Gracefully Close Channels
小结: 1. When a goroutine sends a value to a channel, we can view the goroutine releases the ownership ...
- [Python]编码声明:是coding:utf-8还是coding=utf-8呢
PEP 263 -- Defining Python Source Code Encodings | Python.org https://www.python.org/dev/peps/pep-02 ...
- 转载,Pandas 数据统计用法
pandas模块为我们提供了非常多的描述性统计分析的指标函数,如总和.均值.最小值.最大值等,我们来具体看看这些函数: 1.随机生成三组数据import numpy as npimport panda ...
- Dbeaver 连接 phoenix
Dbeaver 连接 phoenix 1.新建连接 2.选择连接类型Phoenix 3.设置驱动 4.准备驱动包 5.添加驱动 6.添加 Zookeeper Base Path 7.找到驱动类 8.配 ...
- 杂论-FTP
FTP 一 简单介绍 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制文件的双向传输.同时, ...
- 昨晚12点,女朋友突然问我:你会RabbitMQ吗?我竟然愣住了。
01为什么要用消息队列? 1.1 同步调用和异步调用 在说起消息队列之前,必须要先说一下同步调用和异步调用. 同步调用:A服务去调用B服务,需要一直等着B服务,直到B服务执行完毕并把执行结果返回给A之 ...
- 一文弄懂-BIO,NIO,AIO
目录 一文弄懂-BIO,NIO,AIO 1. BIO: 同步阻塞IO模型 2. NIO: 同步非阻塞IO模型(多路复用) 3.Epoll函数详解 4.Redis线程模型 5. AIO: 异步非阻塞IO ...
- Java程序操作Hive
1.hive的lib+jdbc,还要把mysql的连接驱动加载过来 2.编写程序 开启远程服务:[root@zhiyou ~]# hiveserver2 &[1] 4127[root@zhiy ...