On Dec 16, 11:40 pm, Yu-Hsuan Lai <rainco...@gmail.com> wrote:
> I read "Good Parts", and it use Object#beget to implement prototypal
> inheritance.
> beget = function (o) {
>     f = function () {};
>     f.prototype = o;
>     return new f();
>
> }
>
> a = {... object literal ...};
> b = Object.beget(a);
>
> It can build a prototype link when a object is created.
> But if I want to dynamically change the prototype link of a object?
>
> (In fact, I doubt that I really understand what prototype is...)
>
> --
> Lai, Yu-Hsuan

To read Crockford and doubt that one has figured it out is a sign of
intelligence.

1) Start class names with a capital letter if you want to use them
with "new".
2) Functions are Objects. There is nothing you can't do with a
function that you can do with any other Object.
3) That includes assigning properties and methods.
4) This gets interesting when you assign an object to a function's
prototype property.

function Ball( params ) { code }; // later we'll say "var x = new
Ball ..."
Ball.prototype.toString = function () { ... create string here ... };

Ball is a function. Ball is an object. Ball.prototype didn't exist
until we assigned it. Another way of doing that, more typing but maybe
more informative, would be:

Ball.prototype = { toString:function () { ... create string
here ... } };

>From here on, anytime you create a Ball, JavaScript will provide your
ball with a reference to Ball.prototype, where it will look for
anything that it can't find in your Ball's instance.

Assigning methods to functions? Sure.

function outer( params ) {
    code here
    function inner( ... ) { ... }
}

// same as

function outer( params ) {
    code here
}
outer.inner = function (...) { ... }

I wrote a short article on that, here:
http://www.MartinRinehart.com/articles/inner_functions.html

-- 
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