On Thu, Dec 16, 2010 at 11:40 PM, Yu-Hsuan Lai <rainco...@gmail.com> wrote:

> I read "Good Parts", and it use Object#beget to implement prototypal
> inheritance.


Try using (ES5) `Object.create` instead, when available. And preferably not
the one that Doug recommends ;)


> beget = function (o) {
>

Undeclared assignment. That's something to avoid; Oh, and a ReferenceError
in ES5 strict mode.


>     f = function () {};
>

Another undeclared assignment, leaking `f` to the global scope. Prepend
`var` (to make it a function expression) or change to `function f(){}`
(function declaration). Also consider taking `f` outside of "beget" for
performance reasons (idea originally introduced by Cornford few years ago):

var beget = (function() {
  function F(){ };
  return function(o) {
    F.prototype = o;
    return new F;
  };
})();

(you can even give returning function a "name", turning it into a fancy NFE
(named function expression)):

var beget = (function() {
  function F(){ };
  return function beget(o) {
    F.prototype = o;
    return new F;
  };
})();


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

"prototype link" that you're talking about — which I would rather refer to
as [[Prototype]], to stick to proper terminology — is not something that you
can change after object creation. Not in standard ES3 or ES5. You can do it
in a non-standard way. One of them is "__proto__" property, in
implementations which expose [[Prototype]] through it, and which allow
[[Prototype]] to be modified via __proto__ assignment.

[...]

-- 
kangax

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