felix12: *private static* durch umschliessende anonyme *function stacks*

Beitrag lesen

Was du willst ist eine private statische Variabel, ich weiß nicht ob es sowas in Java gibt, in JS kann man sowas konstruieren.

Doch das gibt es in Java natürlich auch :)

Mein anderes Beispiel war falsch. Da mir nich klar ist, wozu du sowas brauchst, hatte ich heute morgen Schwierigkeiten beim umsetzen deiner Gedanken. 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; };
})();


>   
> Struppi.  
  
Danke Struppi, genau sowas habe ich gebraucht wobei die Lösung von von Peter ein bisschen eleganter ist :)  
  

> 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/]]  
> */

Auf sowas wäre ich im Leben nicht gekommen :) Das ist ziemlich genial ! Ehrlich! Trotzdem bin ich mit Javascript ein wenig auf Kriegsfuß weil die OO Umsetzungen ganz anders sind als ich sie aus Java kenne, aber ich hoffe ich werde in diesem Gebiet noch bewanderter :) Ich bin ja noch Jung:)

@ Struppi, Peter - also vielen Dank für eure Hilfe!

0 45

Interne public static member nicht aufrufbar

felix12
  • javascript
  1. 0
    Cheatah
    1. 0
      felix12
      1. 0
        Cheatah
        1. 0
          felix12
          1. 0
            MudGuard
            1. 0
              felix12
              1. 0
                Struppi
                1. 0
                  Patrick Andrieu
                  1. 1
                    peterS.
                    1. 0
                      Patrick Andrieu
                    2. 0
                      felix12
                2. 1

                  *private static* durch umschliessende anonyme *function stacks*

                  peterS.
                  1. 0
                    felix12
                    1. 0
                      Struppi
                      1. 0
                        felix12
                        1. 2

                          klassenlose vollwertige und flexible oo auf funktionaler basis

                          peterS.
                          1. 0
                            felix12
                            1. 0
                              felix 12
                            2. 0
                              Struppi
                              1. 0
                                felix12
                            3. 1

                              vererbung / delegationsprinzipien

                              peterS.
                              1. 0
                                felixcat
                                1. 1

                                  »[Function].arguments.callee« versus »arguments.callee«

                                  peterS.
                                  1. 0
                                    felix12
      2. 0
        Kai345
        1. 0
          felix12
          1. 0
            ChrisB
            1. 0
              felix12
              1. 0
                Struppi
                1. 0
                  felix12
                  1. 0
                    Struppi
                    1. 0
                      felix12
                      1. 0
                        Struppi
                      2. 1

                        einfache objekte/namensraeume und singleton-konstrukte

                        peterS.
                        1. 0
                          Struppi
              2. 2
                Timo "God's Boss" Reitz
                1. 0
                  felix12
                  1. 0
                    Kai345
                    1. 0
                      felix12
                      1. 0
                        Kai345
                        1. 0
                          Kai345
                          1. 0
                            felix12
                            1. 0
                              Kai345
  2. 4
    molily