gruss Struppi, hallo felix12,
...
Was du willst ist eine private statische Variabel, ich weiß nicht,
ob es sowas in Java gibt, in JS kann man sowas konstruieren.... Du willst Zugriff auf ein statische private Variabel über eine
statische Funktion, richtig?Also:
(function() {
var privat = 'hallo';
window.SomeClass = function (){}
SomeClass.hallo = function() { return privat; };
})();
wobei ich auch aus aesthetisch gruenden den zur \*klasse\* hin- oder
besser zum \*private static\* wrapper umgebogenen anonymen funktions-
stack zusammen mit dem konstruktor so abhandeln wuerde, dass es nicht
mehr noetig ist, diesen (den konstruktor) ueber [window] zu adressieren:
~~~javascript
var SomeConstructor = (function () { // kind of *private static* wrapper function
var hallo = "hallo";
var cnstr = (function () {/*
code */
});
cnstr.getHallo = (function () {
return hallo;
});
return cnstr;
})();
var obj = new SomeConstructor();
alert(obj.hallo); // [undefined]
alert(obj.getHallo); // [undefined]
alert(SomeConstructor.getHallo()); // "hallo"
/*
copy und paste nach : [[link:http://jconsole.com/]]
*/
@ alle:
gaebe es darueber hinaus noch eine allgemeine unterstuetzung von
[__defineSetter__] bzw. [__defineGetter__], waere es ein leichtes,
auch noch konstanten, wie z.b. bei [Math] existierend, umzusetzen:
var OtherConstructor = (function () { // kind of *private static* wrapper function
var hallo = "hallo";
var cnstr = (function () {/*
code */
});
cnstr.__defineGetter__("HALLO", (function () { // gecko only
return hallo;
}));
return cnstr;
})();
var obj = new OtherConstructor();
alert(obj.hallo); // [undefined]
alert(obj.HALLO); // [undefined]
alert(OtherConstructor.HALLO); // "hallo"
/*
copy und paste nach : [[link:http://jconsole.com/]]
*/
so long - peterS. - pseliger@gmx.net
--
»Because objects in JavaScript are so flexible, you will want to think differently about class hierarchies.
Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.« - Douglas Crockford
ie:( fl:) br:> va:( ls:& fo:) rl:) n3;} n4:} ss:} de:µ js:} mo:? zu:]
»Because objects in JavaScript are so flexible, you will want to think differently about class hierarchies.
Deep hierarchies are inappropriate. Shallow hierarchies are efficient and expressive.« - Douglas Crockford
ie:( fl:) br:> va:( ls:& fo:) rl:) n3;} n4:} ss:} de:µ js:} mo:? zu:]