Javascript Getter和Setters [英] Javascript Getters And Setters
问题描述
有人可以告诉我有关javascript中的getter和setter的信息吗?什么是吸气剂和吸气剂?我们在哪里可以使用它们?使用它们有什么好处?
Can somebody please tell me about getters and setters in javascript? What are actually getters and setters? Where we can use them? What are the benefits of using them?
推荐答案
Getter
和setter
都是函数.
Getter
将在从具有Getter的变量/对象中检索值时调用.Getter函数必须返回值.
Getter
will call when a value is retrieve from variable/object(which has Getter) Getter function must return value.
var i=count;
如果已经定义了计数的吸气剂,它将调用.
if the count's getter is already defined, it will call.
Setter
将在将值分配给具有Setter的变量/对象时调用
Setter
will call when a value is assign to a variable/object(which has Setter)
count=10
如果已经定义了计数的setter,它将调用.
if the count's setter is already defined, it will call.
看一下这个例子,以便您可以轻松理解Getter
和setter
take a look at this example so that you can easily understand the use of Getter
and setter
这篇关于Javascript Getter和Setters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!