getter-setter

How do I spyOn Typescript getters and setters?

孤街醉人 提交于 2021-02-07 11:38:13
问题 When I unit test my getters are setters for Typescript, I cannot find a way to spy on those getters and setters. Instead, the object immediately gets evaluated. I am using Jasmine to unit test. 回答1: It is not supported yet, but there is a Jasmine issue for supporting getters. If you really need the support now, you can extend SpyRegistry.js file and add the code that apsillers proposed: this.spyOnProperty = function(obj, methodName, accessType) { ... var desc = Object.getPropertyDescriptor

Does modifying the result of a getter affect the object itself?

独自空忆成欢 提交于 2021-02-04 12:56:29
问题 I have a question about using getter methods in java. Suppose I had this class: class Test { private ArrayList<String> array = new ArrayList<String>(); public ArrayList getArray() { return this.array; } public void initArray() { array.add("Test 1"); array.add("Test 2"); } } class Start { public static void main(String args[]) { initArray(); getArray().remove(0); } } My question is: Would the actual arraylist object be modified ("Test 1" removed from it)? I think I have seen this in places,

Pythonic alternative to dict-style setter?

♀尐吖头ヾ 提交于 2021-01-28 05:06:15
问题 People tend to consider getters and setters un-Pythonic, prefering to use @property instead. I'm currently trying to extend the functionality of a class that uses @property to support a dict: class OldMyClass(object): @property def foo(self): return self._foo @foo.setter def foo(self, value): self.side_effect(value) self._foo = value class NewMyClass(object): @property def foo(self, key): # Invalid Python return self._foo[key] @foo.setter def foo(self, key, value): # Invalid Python self.side

Why this JavaScript property returns empty string, while JavaScript function works fine?

梦想的初衷 提交于 2021-01-27 07:22:29
问题 Consider this simple JavaScript module pattern: var human = (function () { var _name = ''; return { name: _name, setName: function (name) { _name = name; } } })(); human.setName('somebody'); alert(human.name); // shows an empty string human = (function () { var _name = ''; return { name: function() { return _name; }, setName: function (name) { _name = name; } } })(); human.setName('somebody'); alert(human.name()); // shows 'somebody' Why the second closure works fine, while the first closure

C++ instance of same class [closed]

心已入冬 提交于 2020-08-15 18:40:47
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 days ago . Improve this question I wondered whether it is possible in C++ to do the same stuff like in Java. That means something like this: public class A { private A a1; private A a2; A getA1(){ return a1; } A getA2(){ return a2; } void setA1(A a1){ this.a1 = a1; } void setA2(A a2){ this.a2 = a2; } } Now I

C++ instance of same class [closed]

a 夏天 提交于 2020-08-15 18:40:21
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 days ago . Improve this question I wondered whether it is possible in C++ to do the same stuff like in Java. That means something like this: public class A { private A a1; private A a2; A getA1(){ return a1; } A getA2(){ return a2; } void setA1(A a1){ this.a1 = a1; } void setA2(A a2){ this.a2 = a2; } } Now I