Re: Very strange problem with Class object
- From: Piotr K <piotr.korzeniewski@xxxxxxxxx>
- Date: Sun, 30 Mar 2008 16:54:16 -0700 (PDT)
On 31 Mar, 01:09, VK <schools_r...@xxxxxxxxx> wrote:
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.
Ups, my mistake, they made it a little diffrent - rather than coping
all attributes they assign them to returned function prototype. So
after change the Class look like this:
var Class = function(tpl) {
var f = function() {
}
f.prototype = tpl;
f.constructor = Class;
return f;
}
I guess this is memory-leek free? :)
.
- Follow-Ups:
- Re: Very strange problem with Class object
- From: Lasse Reichstein Nielsen
- Re: Very strange problem with Class object
- References:
- Very strange problem with Class object
- From: Piotr K
- Re: Very strange problem with Class object
- From: VK
- Re: Very strange problem with Class object
- From: Piotr K
- Re: Very strange problem with Class object
- From: VK
- Very strange problem with Class object
- Prev by Date: Re: getElementByName within containing DIV
- Next by Date: IE and Enter (multi-Input form)
- Previous by thread: Re: Very strange problem with Class object
- Next by thread: Re: Very strange problem with Class object
- Index(es):
Relevant Pages
|