JavaScript Class Getter/Setter - Stack Overflow
文章推薦指數: 80 %
Javascript supports getters and setters. class Form{ set foo(val){ console.log("setting foo") this.fooValue = val; } get foo(){ ...
Home
Public
Questions
Tags
Users
Collectives
ExploreCollectives
FindaJob
Jobs
Companies
Teams
StackOverflowforTeams
–Collaborateandshareknowledgewithaprivategroup.
CreateafreeTeam
WhatisTeams?
Teams
CreatefreeTeam
CollectivesonStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
Learnmore
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
Learnmore
JavaScriptClassGetter/Setter
AskQuestion
Asked
3years,11monthsago
Modified
3years,11monthsago
Viewed
26ktimes
9
1
isthereawaytolistenforapropertycallonaJavaScriptClass
forexamplewhenigosomethinglikethis:
myForm=newForm();
myForm.name='Name';
->whenisetthenameidontonlywanttosetthepropertybutialsowanttoupdatemyVuexstore.
SamethingwithgetiwouldliketoreadfromVuexstore.
IknoewtherearethinslikeProxybutforthisineedtowrapmyClasswithaProxyobject.Notsosureifilikethis.
module.exports=newProxy(newForm({}),{
get(receiver,name){
console.log('gettingpropertyfromVuexStore');
}
});
Whatineedissomethinglikethis:
module.exports=classForm{
//thisshouldbetriggeredwhenform.something
get(property){
returnthis[property];
}
//thisshouldbetriggeredwhenfrom.something='something'
set(property,value){
returnthis[property]=value;
}
};
itthereabestpracticeforthis?
javascriptecmascript-6
Share
Follow
askedApr18,2018at8:43
therabbityouknowtherabbityouknow
19322goldbadges55silverbadges1515bronzebadges
4
1
PossibleduplicateofJavascriptgettersandsettersfordummies?
– notgiorgi
Apr18,2018at8:52
1
@notgiorgi,It'snotaduplicate.TheOPasksaboutsettinggettersandsettersforeveryproperty,eventhosethatarenotdefined.
– StefanOctavian
Apr18,2018at8:55
Don'tuseaproxywhenyouknowthenamesofyourgetters/settersbeforehand.
– Bergi
Apr18,2018at9:13
@StefanOctavianThat'snotclearfromthequestionatall?
– Bergi
Apr18,2018at9:13
Addacomment
|
1Answer
1
Sortedby:
Resettodefault
Highestscore(default)
Datemodified(newestfirst)
Datecreated(oldestfirst)
18
Javascriptsupportsgettersandsetters
classForm{
setfoo(val){
console.log("settingfoo")
this.fooValue=val;
}
getfoo(){
console.log("gettingfoo");
returnthis.fooValue;
}
}
letfrm=newForm();
frm.foo="bar";
console.log(frm.foo);
YoucouldmakethismoredynamicbywritingawithGetterSettermethodwhichwrapseachpropertyofanobjectwithagetter/setter.
varform={
a:"aValue",
b:"bValue"
}
functionwithGetterSetter(obj){
varkeys=Object.keys(obj);
varresult={};
for(vari=0;i
延伸文章資訊
- 1JavaScript Getter and Setter (with Examples) - Programiz
In the above example, the setter method is used to change the value of an object. ... Note: To cr...
- 2一起幫忙解決難題,拯救IT 人的一天
... Know JavaScript [this & Object Prototypes] - Object [番外- getter/setter] ... 我們在Object [下]中有提到...
- 3属性的getter 和setter - 现代JavaScript 教程
访问器属性由“getter” 和“setter” 方法表示。在对象字面量中,它们用 get 和 set 表示:. let obj = { get propName() { // 当读取obj.p...
- 4JS Getter 與Setter DAY71 - iT 邦幫忙::一起幫忙解決難題
Setter 與Setter Getter: 取得特定值的方法Setter: 存值的方法Getter var wallet = { total: 100, set save(price){ th...
- 5[JS] Getter and Setter 筆記 - pcwu's TIL Notes
[JS] Getter and Setter 筆記. 12 Feb 2017. JavaScript. 在JavaScript 中如果Class 在取屬性值或設定屬性值時,如果有比較複雜的運用時...