Rocksolid Light

groups  faq  privacy  How to post  login

Message-ID:  

Try to have as good a life as you can under the circumstances.


rocksolid / de.comp.lang.javascript / Funktion einer Klasse aufrufen als Variablen

SubjectAuthor
* Funktion einer Klasse aufrufen als VariablenJan Novak
+* Re: Funktion einer Klasse aufrufen als VariablenStefan Ram
|`* Re: Funktion einer Klasse aufrufen als VariablenJan Novak
| `* Re: Funktion einer Klasse aufrufen als VariablenStefan Ram
|  `- Re: Funktion einer Klasse aufrufen als VariablenStefan Ram
`- Re: Funktion einer Klasse aufrufen als VariablenStefan Reuther

1
Subject: Funktion einer Klasse aufrufen als Variablen
From: Jan Novak
Newsgroups: de.comp.lang.javascript
Organization: MB-NET.NET for Open-News-Network e.V.
Date: Fri, 3 Nov 2023 12:22 UTC
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: rep...@gmail.com (Jan Novak)
Newsgroups: de.comp.lang.javascript
Subject: Funktion einer Klasse aufrufen als Variablen
Date: Fri, 3 Nov 2023 13:22:00 +0100
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <ui2ol9$1j40p$1@gwaiyur.mb-net.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 3 Nov 2023 12:22:01 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="1675289"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:IAn6eyyEt9xg9GBCg5S6sZ2a2GU= sha256:L+eIkQZ+ExTmzkLJj49Tr1m1eiy5pyszJbkc9/zywbU=
sha1:CDxH3tiLLq5t7QRbbUsokA9GsxI= sha256:HKVKS3GWZ1gweAPGruoB/O8DcoH7JOT0OYfcK/Ggzv0=
Content-Language: de-DE
View all headers

Hallo,

ich beisse mir die Zähne aus an diesem kleinen Beispiel (auch chatGPT
half nicht :-) )
Ich möchte eine Funktion einer Klasse aufrufen, wo sowohl der
Klassenname als auch die Funktion der Klasse als Variablen übergeben werden:

class MyTest {
myTest(data){
console.log("innerhalb MyTest.myTest");
console.log(data);
}
}

const className = "MyTest";
const methodName = "myTest";
const myInstance = new window[className]();
//Ich bekomme hier bereits :Uncaught TypeError: window[className] is not
a constructor

myInstance[methodName](data);

Ein manuelles
const myInstance = new MyTest();
myInstance.myTest("testdaten");
funktioniert

Jan

Subject: Re: Funktion einer Klasse aufrufen als Variablen
From: Stefan Ram
Newsgroups: de.comp.lang.javascript
Organization: Stefan Ram
Date: Fri, 3 Nov 2023 13:24 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: de.comp.lang.javascript
Subject: Re: Funktion einer Klasse aufrufen als Variablen
Date: 3 Nov 2023 13:24:12 GMT
Organization: Stefan Ram
Lines: 44
Expires: 1 Dec 2024 11:59:58 GMT
Message-ID: <objekt-20231103142045@ram.dialup.fu-berlin.de>
References: <ui2ol9$1j40p$1@gwaiyur.mb-net.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de RCzi6gdSBfxQ3QTviU+QhwZbw+/sAFMyfGJIW5cnR6vG3i
Cancel-Lock: sha1:D6dw65f1Co0JBwJAa1SSwa0W5hY= sha256:tKhpTYKltmNa2Ama3JMBoiLWG1n0DZ4Lx+I3ZzP0g9o=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: de-DE-1901
Accept-Language: de-DE-1901, en-US, it, fr-FR
View all headers

Jan Novak <repcom@gmail.com> writes:
>Ich möchte eine Funktion einer Klasse aufrufen, wo sowohl der
>Klassenname als auch die Funktion der Klasse als Variablen
>übergeben werden:

Zeichenfolgen können natürlich mit "eval" ausgeführt werden.
Dies gilt allerdings als nicht besonders elegant und potentiell
unsicher, so daß es heute manchmal auch blockiert wird.

class MyTest
{ myTest( data )
{ console.log( "in MyTest.myTest" );
console.log( data ); }}

const className = "MyTest";

const methodName = "myTest";

eval( "new " + "MyTest" + "()." + "myTest" + "( \"Testdaten\" )" );

Die nächste Möglichkeit kommt ohne "eval" aus, aber sie verlangt
dafür nun ein zusätzliches Objekt "{ MyTest: MyTest }".

class MyTest
{ myTest( data )
{ console.log( "in MyTest.myTest" );
console.log( data ); }}

const className = "MyTest";

const methodName = "myTest";

const classes = { MyTest: MyTest }

const object = new classes[ className ]();

const method = object[ methodName ].bind( object );

method( "Testdaten" );

Anstelle von "classes[ className ]" könnte man auch
"( eval( className ))" schreiben.

Subject: Re: Funktion einer Klasse aufrufen als Variablen
From: Jan Novak
Newsgroups: de.comp.lang.javascript
Organization: MB-NET.NET for Open-News-Network e.V.
Date: Fri, 3 Nov 2023 13:29 UTC
References: 1 2
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!eternal-september.org!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: rep...@gmail.com (Jan Novak)
Newsgroups: de.comp.lang.javascript
Subject: Re: Funktion einer Klasse aufrufen als Variablen
Date: Fri, 3 Nov 2023 14:29:44 +0100
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <ui2sk9$1j5sl$1@gwaiyur.mb-net.net>
References: <ui2ol9$1j40p$1@gwaiyur.mb-net.net>
<objekt-20231103142045@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 3 Nov 2023 13:29:45 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="1677205"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Nl4rg4O1kFUz1SIdrM2NONnCpT8= sha256:XWfsR7ScTLXozPD1eucFLydM2qyPCAECuT/dQn1KLek=
sha1:Eyc92xwjTvFd1uDpf6vIQiLaA2o= sha256:DYLmy5Eh3xnWb6iDQoK0KgtprlPCSo173Hp0uhrY7lo=
Content-Language: de-DE
In-Reply-To: <objekt-20231103142045@ram.dialup.fu-berlin.de>
View all headers

Am 03.11.23 um 14:24 schrieb Stefan Ram:
> Jan Novak <repcom@gmail.com> writes:
>> Ich möchte eine Funktion einer Klasse aufrufen, wo sowohl der
>> Klassenname als auch die Funktion der Klasse als Variablen
>> übergeben werden:
>
> Zeichenfolgen können natürlich mit "eval" ausgeführt werden.
> Dies gilt allerdings als nicht besonders elegant und potentiell
> unsicher, so daß es heute manchmal auch blockiert wird.
....
> eval( "new " + "MyTest" + "()." + "myTest" + "( \"Testdaten\" )" );

OK, das behalte ich mal als Plan Bin der Hinterhand.

> Die nächste Möglichkeit kommt ohne "eval" aus, aber sie verlangt
> dafür nun ein zusätzliches Objekt "{ MyTest: MyTest }".

Aber dieses wäre doch statisch? Das will ich ja auf jeden Fall umgehen.
Oder kann ich dieses Objekt auch mit den Werten aus den "className" und
"methodName" setzen?

Jan

Subject: Re: Funktion einer Klasse aufrufen als Variablen
From: Stefan Ram
Newsgroups: de.comp.lang.javascript
Organization: Stefan Ram
Date: Fri, 3 Nov 2023 13:43 UTC
References: 1 2 3
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: de.comp.lang.javascript
Subject: Re: Funktion einer Klasse aufrufen als Variablen
Date: 3 Nov 2023 13:43:42 GMT
Organization: Stefan Ram
Lines: 14
Expires: 1 Dec 2024 11:59:58 GMT
Message-ID: <Modul-20231103144111@ram.dialup.fu-berlin.de>
References: <ui2ol9$1j40p$1@gwaiyur.mb-net.net> <objekt-20231103142045@ram.dialup.fu-berlin.de> <ui2sk9$1j5sl$1@gwaiyur.mb-net.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de XKTlC48CS7odyv94AW0rXgfv5PLneIJBvG6OdFf19YIQma
Cancel-Lock: sha1:mBu1yLLoOx/siOn/OVshRcvPs5M= sha256:ulgaOPCOA7obIqzHPFArU0tagRETbblBpatiNqMDJ1M=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: de-DE-1901
Accept-Language: de-DE-1901, en-US, it, fr-FR
View all headers

Jan Novak <repcom@gmail.com> writes:
>Aber dieses wäre doch statisch? Das will ich ja auf jeden Fall umgehen.
>Oder kann ich dieses Objekt auch mit den Werten aus den "className" und
>"methodName" setzen?

Wenn die Klassen in einem Modul stehen, das zum Beispiel unter
dem Pfad "./lib" erreichbar ist, dann könnte man "classes"
vielleicht auch etwas dynamischer mit

import * as classes from './lib';

definieren. Dies habe ich hier jedoch nicht getestet.

Subject: Re: Funktion einer Klasse aufrufen als Variablen
From: Stefan Ram
Newsgroups: de.comp.lang.javascript
Organization: Stefan Ram
Date: Fri, 3 Nov 2023 13:59 UTC
References: 1 2 3 4
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: de.comp.lang.javascript
Subject: Re: Funktion einer Klasse aufrufen als Variablen
Date: 3 Nov 2023 13:59:50 GMT
Organization: Stefan Ram
Lines: 31
Expires: 1 Dec 2024 11:59:58 GMT
Message-ID: <this-20231103145908@ram.dialup.fu-berlin.de>
References: <ui2ol9$1j40p$1@gwaiyur.mb-net.net> <objekt-20231103142045@ram.dialup.fu-berlin.de> <ui2sk9$1j5sl$1@gwaiyur.mb-net.net> <Modul-20231103144111@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de fSJlnR60hEFkpPCVCoOf/wAekoacd5lF2VWgss4qqJN+vw
Cancel-Lock: sha1:p9uDEotagVQW1ni7L9tLkQGAGqA= sha256:+NDGDMpOkClWdyVtZqGUuQCTK13aj1wW/07WQV6ClgE=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: de-DE-1901
Accept-Language: de-DE-1901, en-US, it, fr-FR
View all headers

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>Wenn die Klassen in einem Modul stehen

Wenn eine Klasse mit "class" definiert wird, dann steht ihr
Name nicht im globalen Objekt "window" beziehungsweise "this",
sondern im aktuellen Modul.

Falls Du auf Klassen zugreifen willst, deren Namen tatsächlich
in "this" eingetragen sind, wäre Deine ursprüngliche
Vorgehensweise mit "new window[className]();" verwendbar,
wie das folgende Beispiel zeigt.

class MyTest
{ myTest( data )
{ console.log( "in MyTest.myTest" );
console.log( data ); }}

const className = "MyTest";

const methodName = "myTest";

this[ className ]= MyTest

const object = new this[ className ]();

const method = object[ methodName ].bind( object );

method( "Testdaten" );

Subject: Re: Funktion einer Klasse aufrufen als Variablen
From: Stefan Reuther
Newsgroups: de.comp.lang.javascript
Date: Fri, 3 Nov 2023 17:22 UTC
References: 1
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!feeder2.eternal-september.org!eternal-september.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: stefan.n...@arcor.de (Stefan Reuther)
Newsgroups: de.comp.lang.javascript
Subject: Re: Funktion einer Klasse aufrufen als Variablen
Date: Fri, 3 Nov 2023 18:22:56 +0100
Lines: 21
Message-ID: <ui3dq1.25o.1@stefan.msgid.phost.de>
References: <ui2ol9$1j40p$1@gwaiyur.mb-net.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net spxfepQZCazrirtbp/E7WQEwOjb33kUyTR6O3RWAqK9zmKSpgM
Cancel-Lock: sha1:/y2icB4vonNRT1h87LI1wLeoq1w= sha256:T71quBEQP4pOTes1J+KWnXw4dANtt/yOy8A61iUzix0=
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101
Thunderbird/68.12.1 Hamster/2.1.0.1538
In-Reply-To: <ui2ol9$1j40p$1@gwaiyur.mb-net.net>
View all headers

Am 03.11.2023 um 13:22 schrieb Jan Novak:
>     class MyTest {
>         myTest(data){
>             console.log("innerhalb MyTest.myTest");
>             console.log(data);
>         }
>     }
>
>     const className = "MyTest";
>     const methodName = "myTest";
>     const myInstance = new window[className]();
> //Ich bekomme hier bereits :Uncaught TypeError: window[className] is not
> a constructor

Was ist denn `window[className]`? Ist deine Klasse überhaupt im
window-Objekt enthalten, oder liegt die nur in einem lokalen Scope?

Die Browserkonsole sagt zumindest: `new window["Array"]()` funktioniert.

Stefan


rocksolid / de.comp.lang.javascript / Funktion einer Klasse aufrufen als Variablen

1
server_pubkey.txt

rocksolid light 0.9.136
clearnet tor