gruss Christian,
kleines bespiel (was ich meine)
x = 1
y = x(irgendein Operator der mir eine art referenz zu dieser variable gibt)wenn ich nun den wert in y ändere, wird er auch gleichzeitig in x geändert, also
ist x = y = 1
bzw.
x = y = 2
...
also wenn du wissen solltest wie das geht dann sag mir biitteee wie ;)Ich weiss aber worauf du hinaus willst, hatte ich auch schon von gehört (schlechter stil etc.)
was Du moechtest ist in js automatisch fuer
objekte eingebaut, fuer deren ".constructor"-
eigenschaft folgende ausdruecke wahr sind:
- ([object].constructor == Object);
- ([object].constructor == Array);
bsp.:
alert("bsp.\t: [1,2,3]\n\ntype\t: "" + (typeof [1,2,3]) + ""\n\n[1,2,3].constructor == Array == " + ([1,2,3].constructor == Array) + "\n\n\nconstructor:\n" + [1,2,3].constructor.toString());
alert("bsp.\t: new Object()\n\ntype\t: "" + typeof(new Object()) + ""\n\nnew Object().constructor == Object == " + (new Object().constructor == Object) + "\n\n\nconstructor:\n" + new Object().constructor.toString());
die zuweisung eines solchen objekts ist, wie Cheata
schon sagte, _immer_eine_referenz_ - diese objekte
koennen also nicht so ohne weiteres vervielfaeltigt
werden;
fuer die primitiven datentypen "boolean", "number"
und "string" sowie fuer die "object"-typen mit den
konstruktoren "RegExp", "Date", "Error" gilt dies
nicht - "a = datentyp_1" und eine darauffolgende
zuweisung "b = a" wird "b" immer eine vollstaendige
kopie von "a" zuweisen;
funktionen sind ebenfalls von der von Dir gewuenschten
art der refernzierung ausgeschlossen, wenn Du versuchst,
eine funktion mehreren variablen zuzuweisen - ich habe
das gerade ausprobiert:
function fx() {
var fxCounter = 99;
var fyCounter = 11;
alert("private properties:\n\nfxCounter = " + fxCounter + "\n\n\nfyCounter = " + fyCounter);
}
var x = fx;
var y = x;
x();
y(); // bis hierher ist alles klar;
x = null; // referenz waere spaetestens hier weg, wenn es eine gaebe;
y(); // verhaelt sich wie ein aufruf von "fx()" - also keine referenz durch "x";
fx = null;// jetz gibt es auch kein original mehr;
//fx(); // alert: "fx() ist not a function" - sehr schoen;
y(); // verhaelt sich immer noch wie ein aufruf von "fx()";
// demnach wurde die funktion als kopie(y=x;) einer kopie(x=fx;) zugewiesen;
so - und hier mal der code meiner versuchsreihe:
<script type="text/javascript">
<!--
alert("bsp.\t: true\n\ntype\t: "" + (typeof true) + ""\n\ntrue.constructor == Boolean == " + (true.constructor == Boolean) + "\n\n\nconstructor:\n" + true.constructor.toString());
alert("bsp.\t: 1\n\ntype\t: "" + typeof(1) + ""\n\n(1).constructor == Number == " + ((1).constructor == Number) + "\n\n\nconstructor:\n" + (1).constructor.toString());
alert("bsp.\t: "1"\n\ntype\t: "" + (typeof "1") + ""\n\n"1".constructor == String == " + ("1".constructor == String) + "\n\n\nconstructor:\n" + "1".constructor.toString());
alert("bsp.\t: [1,2,3]\n\ntype\t: "" + (typeof [1,2,3]) + ""\n\n[1,2,3].constructor == Array == " + ([1,2,3].constructor == Array) + "\n\n\nconstructor:\n" + [1,2,3].constructor.toString());
alert("bsp.\t: new Object()\n\ntype\t: "" + typeof(new Object()) + ""\n\nnew Object().constructor == Object == " + (new Object().constructor == Object) + "\n\n\nconstructor:\n" + new Object().constructor.toString());
alert("bsp.\t: new Function("",";")\n\ntype\t: "" + typeof(new Function("",";")) + ""\n\nnew Function("",";").constructor == Function == " + (new Function("",";").constructor == Function) + "\n\n\nconstructor:\n" + new Function("",";").constructor.toString());
alert("bsp.\t: /\s/\n\ntype\t: "" + typeof(/\s/) + ""\n\n/\s/.constructor == RegExp == " + (/\s/.constructor == RegExp) + "\n\n\nconstructor:\n" + /\s/.constructor.toString());
alert("bsp.\t: new Date()\n\ntype\t: "" + typeof(new Date()) + ""\n\nnew Date().constructor == Date == " + (new Date().constructor == Date) + "\n\n\nconstructor:\n" + new Date().constructor.toString());
alert("bsp.\t: new Error()\n\ntype\t: "" + typeof(new Error()) + ""\n\nnew Error().constructor == Error == " + (new Error().constructor == Error) + "\n\n\nconstructor:\n" + new Error().constructor.toString());
alert("bsp.\t: Math\n\ntype\t: "" + typeof(Math) + ""\n\nMath.constructor == Math == " + (Math.constructor == Math) + "\n\n\nconstructor:\n" + Math.constructor.toString());
alert("bsp.\t: Function\n\ntype\t: "" + typeof(Function) + ""\n\nFunction.constructor == Function == " + (Function.constructor == Function) + "\n\n\nconstructor:\n" + Function.constructor.toString());
function getLogText(theObject) {
if ((theObject === null) || (typeof(theObject) == "undefined")) {
return typeof(theObject);
} else {
var logText = "";
var propertyName = "";
for (propertyName in theObject) {
logText += propertyName + " = " + theObject[propertyName] + "\n";
}
return logText;
}
}
var x,y;
// ASSIGNING == REFERENCING
// ASSIGNING == REFERENCING
//
// OBJECT
x = {value1:"irgendwas",value2:true,"value3":24};
y = x;
alert("x - logData:\n\n" + getLogText(x) + "\ny - logData:\n\n" + getLogText(y));
alert("y - logData:\n\n" + getLogText(y) + "\nx - logData:\n\n" + getLogText(x));
y.value3 = "ganzwasanderes";
y["value1"] = "irgendwasanderes";
y.value5 = new Object();
alert("Object: assigning == referencing:\n\nx - logData:\n\n" + getLogText(x) + "\ny - logData:\n\n" + getLogText(y));
alert("Object: assigning == referencing:\n\ny - logData:\n\n" + getLogText(y) + "\nx - logData:\n\n" + getLogText(x));
//
// ARRAY
x = [1,2,3];
y = x;
alert("x = [" + x.join(",") + "]\ny = [" + y.join(",") + "]");
y[2] = "aber";
y[3] = "hallo";
alert("Array: assigning == referencing:\n\nx = [" + x.join(",") + "]\ny = [" + y.join(",") + "]");
//
// ASSIGNING != REFERENCING
// ASSIGNING != REFERENCING
//
// FUNCTION
function fx() {
var fxCounter = 99;
var fyCounter = 11;
alert("private properties:\n\nfxCounter = " + fxCounter + "\n\n\nfyCounter = " + fyCounter);
}
var x = fx;
var y = x;
alert(getLogText(x));
alert(getLogText(y));
x();
y();
x = null;
alert(getLogText(x));
alert(getLogText(y));
y();
fx = null;
//fx();
alert(getLogText(fx));
alert(getLogText(y));
y();
//
// REGEXP
x = /\s/g;
y = x;
alert("x = " + x + "\ny = " + y);
x = "was anderes";
alert("RegExp: assigning != referencing:\n\nx = " + x + "\ny = " + y);
//y = /<([^>]*)>/;
//alert("RegExp: assigning != referencing:\n\nx = " + x + "\ny = " + y);
//
// DATE
x = new Date().toLocaleString();
y = x;
alert("x = " + x + "\ny = " + y);
x = "was anderes";
alert("Date: assigning != referencing:\n\nx = " + x + "\ny = " + y);
//y = "was anderes";
//alert("Date: assigning != referencing:\n\nx = " + x + "\ny = " + y);
//
// ERROR
x = new Error();
y = x;
alert("x = " + x + "\ny = " + y);
x = "was anderes";
alert("Error: assigning != referencing:\n\nx = " + x + "\ny = " + y);
//y = "was anderes";
//alert("Error: assigning != referencing:\n\nx = " + x + "\ny = " + y);
//-->
</script>
by(t)e by(t)e - peterS. - pseliger@gmx.net
--
sh:| fo:) ch:? rl:| br:& n3:} n4:# ie:| mo:{ va:| de:[ zu:] fl:) ss:) ls:& js:)