On 23.12.2010 11:49, Yu-Hsuan Lai wrote:
At my first sight, I were amazed at the prototypal concept in "Good Part".
But when I tried to write little(very little) code, I'm confused.
Deeper I think about the skill, it looks more similar to classical...
My feeling is caused by a lack of understanding?

/Technically/, there is /no/ a big difference. Especially, if the prototype chain doesn't change during the run-time, the resolution difference is even less:

Prototypal: own -> proto -> proto -> proto -> null
Classical (abstractly): own -> class -> superclass -> superclass -> null.

The only big difference is /ideological/. In case of classical approach the inheritance chain is linear (it's called a /tower/, a /vertical code reuse/) and /systematized/ (or classified). In case of prototypal inheritance -- the chain is not systematized (an object may inherit from any object, which in turn inherits from another object -- at any step this combination may rearrange and regroup in contrast with the classical approach). There are no other very important differences (all the other differences are cosmetic -- a needed sugar).

Notice, that in prototypal inheritance the inheritance chain is also linear (as shown above). The ability to add a /horizontal code reuse/ (i.e. at every link of the inheritance chain, the link inherits something /horizontally/) is provided by the /traits/ and /mixins/. Thus, again, it may be done for both -- classical and prototypal systems.

own -> proto -> proto -> proto -> null
              |              |             |
              a             c            b
              |                            |
              b                           a

These a, b and c objects are /mixed/ to needed objects in the inheritance chain. And again, at the end, one of the resolution technique is make this chain as linear:

own -> proto -> a -> b -> proto -> c -> proto -> b -> a -> null

Absolutely the same can be done in classical approach (examples: Python, Ruby).

I.e. the presence of the concept of a /class/ doesn't change much if internally the /same techniques/ are used. A good example again is Python -- it's a delegation based (the same as JS!) language, where concept of a class is just a /syntactic sugar/. To see how this sugar is implemented consider e.g. CoffeeScript which sugars the JS's constructor+proto pair.

Are there some excellent open source project I should read to understand javascript?

I can recommend my "Chapter 7. OOP." (there are two lectures "7.1. The General Theory" where /all/ needed aspects are considered and "7.2. ECMAScript implementation" devoted to exactly JS) of ES3 series. And (as a brief but detailed lecture) "JavaScript. The Core."


Dmitry.

(Hmm... I know it's impossible to not open javascript's source code :)

On Thu, Dec 23, 2010 at 4:37 PM, Yu-Hsuan Lai <[email protected] <mailto:[email protected]>> wrote:

    In fact, even though I just want some public variants, I still
    have the same problem.
    var obj = {a:1,b:2,c:3 ... some methods...};
    obj2 = Object.create(obj);
    obj2.a is referred to obj.a, right? If I want another instance, I
    have to write obj2.a = 1;
    But if the object is very "big"? Write a function to assign all
    variants? Is it "constructor"?
    Once I implement these all, my prototypal OO still is different
    from classical OO?

    On Thu, Dec 23, 2010 at 3:14 PM, אריה גלזר <[email protected]
    <mailto:[email protected]>> wrote:

        This is exacatly the point - you create the methods and the
        variable together, so either you get a copy of all of them, or
        you get a new instance.
        But if you want a 'private' variable for each instance, the
        only way you are going to achieve this (I think) is by
        creating a separate closure for each object creation. So you
        can either do the above second solution, or you can do
        var obj = {
                getA : function getA() {
                    return this.a;
                },
                setA : function setA(b) {
                    a = this.b;
                }
            };

        function F(){ this.a = 'a';}
        for (i =0; i<10;i++) x.push((function(){ F.prototype = obj;
        return new F();})();

        And you will still be exposing a in the end. But the point is,
        this is much less readable and performance-wise I don't think
        it really matters, so your second pattern is good enough IMO.

        note - this list beeing so heavy on js wizards, I'm always a
        little afraid of posting comments here...


        On Thu, Dec 23, 2010 at 8:23 AM, Yu-Hsuan Lai
        <[email protected] <mailto:[email protected]>> wrote:

            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/[email protected]/

            To search via a non-Google archive, visit here:
            http://www.mail-archive.com/[email protected]/

            To unsubscribe from this group, send email to
            [email protected]
            <mailto:jsmentors%[email protected]>




-- Arieh Glazer
        אריה גלזר
        052-5348-561
        http://www.arieh.co.il
        http://www.link-wd.co.il

-- To view archived discussions from the original JSMentors
        Mailman list: http://www.mail-archive.com/[email protected]/

        To search via a non-Google archive, visit here:
        http://www.mail-archive.com/[email protected]/

        To unsubscribe from this group, send email to
        [email protected]
        <mailto:jsmentors%[email protected]>




-- Lai, Yu-Hsuan




--
Lai, Yu-Hsuan
--
To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to