草庐IT

Getters-Setter

全部标签

javascript - CoffeeScript:对象初始化器中的 Getter/Setter

ECMAScript允许我们如下定义getter或setter:[文本/javascript]varobject={property:7,getgetable(){returnthis.property+1;},setsetable(x){this.property=x/2;}};如果我使用的是类,我可以变通:[文本/coffeescript]"usestrict"Function::trigger=(prop,getter,setter)->Object.defineProperty@::,get:getterset:setterclassClassproperty:''@trigg

javascript - 如何在原型(prototype)上定义 setter/getter

2016年10月编辑:请注意这个问题是在2012年提出的。每个月左右都会有人添加一个新的答案或评论来反驳一个答案,但这样做并没有什么意义,因为这个问题可能已经过时了(记住,它是为GnomeJavascript编写gnome-shell扩展,而不是浏览器的东西,这是非常具体的)。正在关注mypreviousquestion关于如何在Javascript中进行子类化,我正在创建父类(superclass)的子类,如下所示:functioninherits(Child,Parent){varTmp=function{};Tmp.prototype=Parent.prototype;Child

javascript - 是否可以在 JavaScript 中实现动态 getters/setters?

我知道如何为已知名称的属性创建getter和setter,方法如下://Atrivialexample:functionMyObject(val){this.count=0;this.value=val;}MyObject.prototype={getvalue(){returnthis.count"Goaway"a.value='bar';alert(a.value);//-->"bar2"现在,我的问题是,是否可以定义像这样的包罗万象的getter和setter?即,为任何未定义的属性名称创建getter和setter。这个概念可以在PHP中使用__get()和__set()魔术方

javascript - 傻瓜 setter/getter \ setter/getter

我一直在努力了解getter和setter,但没有深入了解。我读过JavaScriptGettersandSetters和DefiningGettersandSetters只是没有得到它。谁能说清楚:getter和setter的用途,以及举一些非常简单的例子? 最佳答案 除了@millimoose'sanswer,setter也可用于更新其他值。functionName(first,last){this.first=first;this.last=last;}Name.prototype={getfullName(){returnt

go - 避免在 Go 中编写过多的 getter 和 setter

我正在用Go实现一个消息传递系统。所以我有一个名为Msg的通用接口(interface)。Msg接口(interface)定义了许多常用字段,例如源、目标、发送时间、接收时间等。我无法定义完整的Msg列表,因为我想要库用户定义Msg的具体类型。要提供具体类型的Msg,用户需要实现大量的getter和setter,这非常烦人。我尝试的一个解决方案是提供一个简单的基类,如MsgBase并定义所有公共(public)属性以及getter和setter。对于每个具体类型的Msg,我都嵌入了一个指向MsgBase的指针。该解决方案有效。但是,我想在具体的Msg类型中嵌入MsgBase的值版本。这

pointers - Setter 方法不设置结构属性 Golang

我需要帮助来理解为什么会抛出这个错误:我正在使用指针,因为我希望它更新字段。prog.go:56:cannotuseMammalImplliteral(typeMammalImpl)astypeMammalinarrayelement:MammalImpldoesnotimplementMammal(SetNamemethodhaspointerreceiver)prog.go:57:cannotuseMammalImplliteral(typeMammalImpl)astypeMammalinarrayelement:MammalImpldoesnotimplementMammal(

go - 如何在 Golang 中实现导出字段的 setter?

我正在使用gorm库,要使用该库,我必须导出所有列字段。像这样typemyTypestruct{IdintNamestring}但是,我该如何处理结构字段的附加逻辑。比方说,如果我想在Name字段前加上Mr怎么办?客户端始终可以执行myType.Name="whatever"。如果我将Name设置为未导出并为其设置了setter,则该字段将永远无法与gorm一起使用。有什么golang方法可以解决这个问题吗? 最佳答案 您对未导出字段的理解是正确的。您的struct字段必须导出,然后只有gorm可以访问它。这就是Go方式。所以你有2

go - 在 Go 中命名 Getters 的惯用方式

Effectivego对getter的命名有以下建议:Godoesn'tprovideautomaticsupportforgettersandsetters.There'snothingwrongwithprovidinggettersandsettersyourself,andit'softenappropriatetodoso,butit'sneitheridiomaticnornecessarytoputGetintothegetter'sname.Ifyouhaveafieldcalledowner(lowercase,unexported),thegettermethods

go - Go 中的 Getter 和 Setter 约定

案例A不遵循Getter&Setter约定人类/人类.gopackagehumantypeHumaninterface{GetName()stringSetName(namestring)}typePersonstruct{Namestring}func(pPerson)GetName()string{returnp.Name}func(p*Person)SetName(namestring){p.Name=name}主要/main.gopackagemainfuncmain(){john:=Person{Name:"john"}//UppercaseFieldsarevisiblef

struct - 对结构类型使用 setter 无法按预期工作

对结构使用setter函数,但未按预期工作:packagemainimport"fmt"typeTstruct{Valstring}//thissetterseemsnottoworkfunc(tT)SetVal(sstring){t.Val=s}//thissetter,usingptrtoT,seemstoworkokfunc(t*T)SetVal2(sstring){(*t).Val=s}funcmain(){v:=T{"abc"}fmt.Println(v)//prints{abc}v.SetVal("pdq")fmt.Println(v)//prints{abc},wasex