getter-setter

Why use getters and setters in JavaScript? [closed]

荒凉一梦 提交于 2019-11-28 21:05:49
I know how getter and setter work in JavaScript. What I don't understand is why we need them when we can get the same result using normal functions? Consider the following code: var person = { firstName: 'Jimmy', lastName: 'Smith', get fullName() { return this.firstName + ' ' + this.lastName; } } console.log(person.fullName); // Jimmy Smith We can easily replace getter with a function: var person = { firstName: 'Jimmy', lastName: 'Smith', fullName: function() { return this.firstName + ' ' + this.lastName; } } console.log(person.fullName()); // Jimmy Smith I don't see the point of writing

Python: multiple properties, one setter/getter

早过忘川 提交于 2019-11-28 19:37:20
Consider the following class definitions class of2010(object): def __init__(self): self._a = 1 self._b = 2 self._c = 3 def set_a(self,value): print('setting a...') self._a = value def set_b(self,value): print('setting b...') self._b = value def set_c(self,value): print('setting c...') self._c = value a = property(fset=self.set_a) b = property(fset=self.set_b) c = property(fset=self.set_c) note that set_[a|b|c]() do the same thing. is there a way do define: def set_magic(self,value): print('setting <???>...') self._??? = value once and use it for a,b,c as follows a = property(fset=self.set

Is it good practice to make getters and setters inline?

五迷三道 提交于 2019-11-28 17:42:22
public: inline int GetValue() const { return m_nValue; } inline void SetValue(int nNewValue) { this -> m_nValue = nNewValue; } On Learn C++ , they said it would run faster. So, I thought it would be great to use on getters and setters. But maybe, there are some drawbacks to it? I don't inline anything until a profiler has specifically told me that not inlining is resulting in a performance problem. The C++ compiler is very smart and will almost certainly automatically inline such simple function like this for you. And typically it's smarter than you are and will do a much better job at

Cross-browser Getter and Setter

一个人想着一个人 提交于 2019-11-28 17:38:09
问题 This works in modern Chrome/Firefox/Opera but fails in IE8. Haven't tried it in IE9. How can I make this cross-browser compatible, including IE7+? (Fiddle here.) var foo = { get test(){ return 'Works'; } }; // foo.test should be 'Works' I've seen some usage with __defineGetter__ but that threw an 'unrecognized method' error in IE8. 回答1: I don't believe you can. In IE8 and lower, property access is mere property access. There's no way to run function code without explicitly invoking the

Custom Getter & Setter iOS 5

跟風遠走 提交于 2019-11-28 17:13:44
I want to override the getter and setter in my ObjC class using ARC. .h File @property (retain, nonatomic) Season *season; .m File @synthesize season; - (void)setSeason:(Season *)s { self.season = s; // do some more stuff } - (Season *)season { return self.season; } Am I missing something here? Yep, those are infinite recursive loops. That's because self.season = s; is translated by the compiler into [self setSeason:s]; and return self.season; is translated into return [self season]; Get rid of the dot-accessor self. and your code will be correct. This syntax, however, can be confusing given

Getter/setter in constructor

孤街浪徒 提交于 2019-11-28 17:02:03
I recently read about the fact that there is a possibility of defining getters/setters in JavaScript. It seems extremely helpful - the setter is a kind of 'helper' which can parse the value to be set first, before actually setting it. For example, I currently have this code: var obj = function(value) { var test = !!value; // 'test' has to be a boolean return { get test() { return test }, set test(value) { test = !!value } }; }; var instance = new obj(true); This code always converts value to a boolean. So if you code instance.test = 0 , then instance.test === false . However, for this to work

How can I overwrite a getter method in an ActiveRecord model?

谁说我不能喝 提交于 2019-11-28 16:55:43
问题 I'm trying to overwrite a getter method for an ActiveRecord model. I have an attribute called name in the model Category , and I'd like to be able to do something like this: def name name_trans || name end If name_trans attribute is not nil, then return it, else return name attribute. How would I do this? This should then be called normally like this: @category.name 回答1: The Rails Style Guide recommends using self[:attr] over read_attribute(:attr) . You can use it like this: def name name

How do you implement a private setter when using an interface?

与世无争的帅哥 提交于 2019-11-28 16:53:24
问题 I've created an interface with some properties. If the interface didn't exist all properties of the class object would be set to { get; private set; } However, this isn't allowed when using an interface,so can this be achieved and if so how? 回答1: In interface you can define only getter for your property interface IFoo { string Name { get; } } However, in your class you can extend it to have a private setter - class Foo : IFoo { public string Name { get; private set; } } 回答2: Interface defines

What are the differences amongst Python's “__get*__” and “_del*__” methods?

巧了我就是萌 提交于 2019-11-28 16:01:45
问题 I just started learning Python a few months ago, and I'm trying to understand the differences between the different __get*__ methods: __get__ __getattr__ __getattribute__ __getitem___ And their __del*__ equivalents: __del__ __delattr__ __delete__ __delitem__ What are the differences between these? When should I use one over the other? Is there a specific reason why most of the __get*__ methods have __set*__ equivalents, but there is no __setattribute__ ? 回答1: The documentation for every

Adding a getter makes using an underscore incorrect syntax

流过昼夜 提交于 2019-11-28 14:07:02
I have a class with the following header: #import <Foundation/Foundation.h> @interface CustomClass : NSObject @property (strong, nonatomic) NSString *foo; @end With the following implementation that does not show any errors: #import "CustomClass.h" @implementation CustomClass - (void) setFoo:(NSString *)foo { _foo = foo; } @end Being a complete beginner to Objective-C, I am baffled when I add the following method to the implementation: - (NSString *)foo { return _foo; } because now there is an error in the method use of undeclared identifier 'title' and it recommends that I change _foo to foo