Re: Very strange problem with Class object



On Mar 31, 2:53 am, Piotr K <piotr.korzeniew...@xxxxxxxxx> wrote:
On 31 Mar, 00:39, VK <schools_r...@xxxxxxxxx> wrote:



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.

It creates classes, the same way that ie. mooTools does, I just
shortened it for clarity.. however, I managed to solve my problem ;)

You did? OK, enjoy your program working with a memory monitor.

btw. mooTools Class suffers from the same problem which kind a
suprised me.

You mean mooTools cut a deal with the Spirit of Programming so memory
leaks and closures should not be applicable to it no matter how bad
the code is? :-) :-|

My heart is bleeding by looking at this code. Maybe you could take
Prototype.js and its pattern instead? It still imitating class-based
paradigm but it has some respect to the real nature of the used
language so it does not leak 1Mb per 50Kb of code.

Up to you, anyway.
.