Re: Very strange problem with Class object



On Mar 31, 2:22 am, Piotr K <piotr.korzeniew...@xxxxxxxxx> wrote:
I simply ran out of ideas why this piece of code works so strange,
it's a very simple class creating object:

var Class = function(tpl) {
return function() {
for(var t in tpl) this[t] = tpl[t];
};

}

var X = new Class({
arr: []

});

var x = new X;
x.arr.push(5);

var y = new X;
alert(y.arr[0]) // displays 5

How, I mean HOW in the world it displays 5? I tested it with normal
string and number variables - works fine, but I can't figure what's
wrong with the arrays? I tried to change the Class function in many
ways like making "new Object(tpl[t])" or "tpl[t].valueOf()" but
nothing helped.. I'm frustrated, it's so freakin strange..

Because you have created a very efficient memory leak over closure
with tpl value retained in it. I honestly have no idea what does this
code suppose to mean. Is it some work-on-denial test for a script
engine? There are more sophisticated professional packages for that,
Acid 3 from the first coming into my mind.
.



Relevant Pages

  • scroll select
    ... arr = getMultiple; ... var keep_array = new Array; ... Prev by Date: ...
    (comp.lang.javascript)
  • Re: Radio Buttons
    ... Dr J R Stockton wrote on 13 mrt 2008 in comp.lang.javascript: ... function RadBtns(Rbtn, Arr) { ... var Q, J = -1; ... This will return 'undefined' when no radiobutton is checked ...
    (comp.lang.javascript)
  • Re: Very strange problem with Class object
    ... var X = new Class({ ... You assign a reference to an array to its 'arr' property when your ... Using - push - on an array is modifying that array. ...
    (comp.lang.javascript)
  • Re: Array to Object
    ... Eli wrote: ... Having this array: ... $arr = array( ... How can I access $arr['my var'] in $obj? ...
    (php.general)
  • Question about function objects vs object literals?
    ... function TestObj(src, dest){ ... and here is how I'm using the above (using JQuery) ... var t=new TestObj; ... Any ideas on how to make METHOD C work while avoiding a closure. ...
    (comp.lang.javascript)