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函数原理来 ...
随机推荐
- RPC 接口必须是业务职责
https://mp.weixin.qq.com/s/MYSF8lCF92ItG_Lc8nOspg 一个加班多新人多团队,我们的代码问题与重构 陈于喆 高可用架构 2020-10-21 微服务编码 ...
- 我感兴趣的 .NET 开源项目
Gui.cs - 用于.NET 的控制台终端 UI 工具包 https://github.com/migueldeicaza/gui.cs Newtonsoft.Json - 高性能的 JSON 解析 ...
- TCP/IP网络中的显式拥塞通告(ECN)
当前的TCP 实现将TCP 端节点之间的中间网络视为一个不透明的"黑盒".TCP 包进入和流出这个盒子.有些时候进入盒子的包被丢失了.因为今天的数字和光媒体上出现比特级错误的机会非 ...
- PowerApps画布应用编码规范 和指南
花了一番功夫把PowerApps编码最佳实践的官方白皮书本地化了一下,顺便对部分产品变更做了校对和注释,欢迎大家查阅和分享. 从我实际的项目实施经验来讲,内容还是值得一读,可以帮助项目更好维护和管理. ...
- 远程url文件地址转成byte
public static byte[] urlTobyte(String url) throws MalformedURLException { URL ur = new URL(url); Buf ...
- 基于efcore的分表组件开源
ShardingCore ShardingCore 是一个支持efcore 2.x 3.x 5.x的一个对于数据库分表的一个简易扩展, 目前该库暂未支持分库(未来会支持),仅支持分表,该项目的理念是让 ...
- 一个ftp协议传输文件之后执行脚本无法工作的情况
作者:良知犹存 转载授权以及围观:欢迎添加微信号:Conscience_Remains 总述 移植一个文件系统时候,我在window下git clone了对方仓库源码,然后用FileZ ...
- Codeforces Round #589 (Div. 2) Another Filling the Grid (dp)
题意:问有多少种组合方法让每一行每一列最小值都是1 思路:我们可以以行为转移的状态 附加一维限制还有多少列最小值大于1 这样我们就可以不重不漏的按照状态转移 但是复杂度确实不大行(减了两个常数卡过去的 ...
- Educational Codeforces Round 84 (Div. 2)
Educational Codeforces Round 84 (Div. 2) 读题读题读题+脑筋急转弯 = =. A. Sum of Odd Integers 奇奇为奇,奇偶为偶,所以n,k奇偶性 ...
- Codeforces Round #645 (Div. 2) D. The Best Vacation (贪心,二分)
题意:一年有\(n\)个月,每月有\(d_{i}\)天,找出连续的\(x\)天,使得这\(x\)天的日期总和最大,任意一年都能选. 题解:首先要先贪心,得到:连续的\(x\)天的最后一天一定是某个月的 ...