private-members

JS: revealing module pattern - accessing internal objects vs arrays?

自闭症网瘾萝莉.ら 提交于 2020-01-25 12:24:05
问题 Using the revealing module pattern, how can I provide direct access to non-static private variables? Here's what I have: var M = function () { var obj = {}; var arr = []; var change = function () { obj = {"key":"if I see this, O is a reference to obj"}; arr.push("If I see this, A is a reference to arr") }; return { change: change, O: obj, A: arr }; }(); M.change(); console.log(M.A); // prints ["If I see this, A is a reference to arr"] console.log(M.O); // prints Object {}, wanted "if I see

How to make method private and inherit it in Coffeescript?

柔情痞子 提交于 2020-01-19 17:18:21
问题 How to make method "btnClick" private? class FirstClass constructor: -> $('.btn').click @btnClick btnClick: => alert('Hi from the first class!') class SecondClass extends FirstClass btnClick: => super() alert('Hi from the second class!') @obj = new SecondClass http://jsfiddle.net/R646x/17/ 回答1: There's no private in JavaScript so there's no private in CoffeeScript, sort of. You can make things private at the class level like this: class C private_function = -> console.log('pancakes') That

Private static variables in php class

本秂侑毒 提交于 2020-01-13 08:33:32
问题 I have a few classes that are often run through var_dump or print_r . Inside these classes I have a few variables that are references to other, rather large objects that only ever has one instance of each and are only used inside the classes (outside the classes have their own reference to these classes) I do not wish these classes printed in the output, so I have declared them as private static which is working fine. But my IDE (PHPstorm) is flicking up an error-level alert with Member has

python - why is read-only property writable?

旧城冷巷雨未停 提交于 2020-01-12 06:48:13
问题 I am trying to define a class with a read-only property in a Python; I followed Python documentation and came up with the following code: #!/usr/bin/python class Test: def __init__(self, init_str): self._prop = init_str @property def prop(self): return self._prop t = Test("Init") print t.prop t.prop = "Re-Init" print t.prop Now when I try to execute the code though I am expecting error/exception I see it getting executed normally: $ ./python_prop_test Init Re-Init My Python version is 2.7.2.

Why does C++ allow private members to be modified using this approach?

荒凉一梦 提交于 2020-01-09 05:21:46
问题 After seeing this question a few minutes ago, I wondered why the language designers allow it as it allows indirect modification of private data. As an example class TestClass { private: int cc; public: TestClass(int i) : cc(i) {}; }; TestClass cc(5); int* pp = (int*)&cc; *pp = 70; // private member has been modified I tested the above code and indeed the private data has been modified. Is there any explanation of why this is allowed to happen or this just an oversight in the language? It

Proper way to declare and set a private final member variable from the constructor in Java?

安稳与你 提交于 2020-01-04 05:33:14
问题 There are different ways to set a member variable from the constructor. I am actually debating how to properly set a final member variable, specifically a map which is loaded with entries by a helper class. public class Base { private final Map<String, Command> availableCommands; public Base() { availableCommands = Helper.loadCommands(); } } In the above example the helper class looks like this: public class Helper { public static Map<String, Command> loadCommands() { Map<String, Command>

Delphi: writing to the private ancestor's field in descendant class

大兔子大兔子 提交于 2020-01-02 01:07:36
问题 I need to fix a third-party component. This component's class has private variable which is actively used by its descendants: TThirdPartyComponentBase = class private FSomeVar: Integer; public ... end; TThirdPartyComponent = class (TThirdPartyComponentBase) protected procedure Foo; virtual; end; procedure TThirdPartyComponent.Foo; begin FSomeVar := 1; // ACCESSING PRIVATE FIELD! end; This works because both classes are in the same unit, so they're kinda "friends". But if I'll try to create a

Proper Objective-C Helper “Wannabe” Private methods?

青春壹個敷衍的年華 提交于 2020-01-01 00:45:56
问题 While I hate to beat a horse to death on this subject (I've read through various articles about this), but would just like to get more opinions on this matter before I create my "own convention" to use from now on while coding in Objective-C. The convention that I want to figure out is ultimately how to (using best coding practices for production level code) use private methods in a class. Coming from a background in C#, when I write classes, usually there is a block of code that is repeated

Deleting object with private destructor

a 夏天 提交于 2019-12-30 06:12:54
问题 How is that possible that it is allowed to delete object with private destructor in the following code? I've reduced real program to the following sample, but it still compiles and works. class SomeClass; int main(int argc, char *argv[]) { SomeClass* boo = 0; // in real program it will be valid pointer delete boo; // how it can work? return -1; } class SomeClass { private: ~SomeClass() {}; // ! private destructor ! }; 回答1: You are trying to delete object of incomplete class type. C++ Standard

Access unexported fields in golang/reflect?

旧巷老猫 提交于 2019-12-30 04:29:08
问题 Is there a way to use Reflect to access unexported fields in go 1.8? This no longer seems to work: https://stackoverflow.com/a/17982725/555493 Note that reflect.DeepEqual works just fine (that is, it can access unexported fields) but I can't make heads or tails of that function. Here's a go playarea that shows it in action: https://play.golang.org/p/vyEvay6eVG. The src code is below import ( "fmt" "reflect" ) type Foo struct { private string } func main() { x := Foo{"hello"} y := Foo{"goodbye