I'm trying use prototypal inheritance instead of classical one. But I'm
still confused.
I can't complete very very very small tasks, like this one:
Create 10 copies of a object(with a private variant and public functions to
access it) in an array.

I have two way to approach it, first is to use Object#create:
var x=[];
x[0]=(function () {
    var a=10;
    return {
        getA : function getA() {
            return a;
        },
        setA : function setA(b) {
            a = b;
        }
    };
})();
for(var i=1; i<10; i++)
    x[i] = Object.create(x[0]);

But all 10 objects' "a"s refer to a single integer. Tragedy.
My second way is call a function which return a object 10 times:
function createX() {
    var a=10;
    return {
        getA : function getA() {
            return a;
        },
        setA : function setA(b) {
            a = b;
        }
    };
}
var x=[];
for(var i=0; i<10; i++)
    x[i] = createX();

It works. But every x has its own "getA" and "setA" instance. In contrast to
the former, it costs more memory.
I know it maybe doesn't matter. But knowing prototypal OO can use only one
instance, creating 10 let me regard me as a stupid.

Except the two methods, the only one method I can figure out is... classical
OO.
Is it avoidable?



-- 
Lai, Yu-Hsuan

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to