1

I rather like the prototype way of programming and have been trying to understand it in javascript.

I saw this bit of code in The Good Parts:

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

I don't get this at all lol. If all you have to do is set the prototype to a past object, then couldn't you just do this:

var parent = {
  num = 66;
};
var child = {
  prototype: parent
};

This doesn't seem to work though, cause child.num is returned as undefined. How do you describe javascript prototype programming and what are your methods? Thanks guys

1
  • I think I really confusing objects and functions I've discovered. Though I'm not sure why there is needed a difference. Either way, it's be cool and simpler if there was no difference. I need time to meditate on this Commented May 6, 2011 at 23:20

5 Answers 5

2

Personally I think the most idiomatic method is as follows:

function Parent() {
    this.value = 2;
}

function Child() {

}

Child.prototype = new Parent();

var c = new Child();
alert(c instanceof Child); // true
alert(c instanceof Parent); // true
alert(c.value); // 2
Sign up to request clarification or add additional context in comments.

4 Comments

Don't forget to augment the prototype.constructor property to point to Child too :)
this doesn't seem to work for me :/ when I do this c.value is undefined
@Isaiah - I've run the code in all major browsers and it gave the expected result each time.
It seems like classes though, is there a way of doing this without having to use new? Isn't there a way where you could just do Child.value?
1

You can only add prototype to a function object. When invoked via new it will use it as its prototype.

Btw, the function you quoted is part of the new version of ECMAScript as Object.create (with an additional propertiesObject parameter).

Let me put it this way: {object} is a singleton. A function() object however is a constructor, that is, when called with new creates an instance (by executing the function's body, and using the constructor's prototype). Of course the prototype can be a function object too, and have its own prototype, etc. Specialization and Generalization means walking up and down the prototype chain.

8 Comments

so... you can only inherit from function=>function? that seems stupid
@Isaiah - That's not a good way to think about it. An object inherits the props/methods of the function used to create the object. It is actually a very powerful paradigm, once you get your mind around.
but why can't an object inherit from another object? seems limiting, I'm trying to get my mind around it I swear lol
functions are objects, don't just think of them as standalone entities that you can call :) You might be interested in Crockford's "Function - the Ultimate" talk - developer.yahoo.com/yui/theater/video.php?v=crockonjs-3
I wish I could understand this, but I'm not able to at this time. I'll just use it till I can understand it. Thanks for letting me know this exists in the standard now.
|
1

The second example does not work because your just setting a property of an object. What you want to do is set the prototype property of a constructor function, and then create an object from that function.

var parent = {
   num : 666
};

function ChildConstructor(){
   console.log(this.num);
}
ChildConstructor.prototype = parent;

var child = new ChildConstructor();

http://jsfiddle.net/W9C3K/

Setting or changing the prototype on an object is not possible without proprietary methods.

2 Comments

just curious, why is it necessary to use new? seems to increase code bloat to me, isn't there a ways to just make ChildConstructor.num work?
The simple answer: you could not create multiple objects of the ChildConstructor withouth new (o: The kinda complex answer: This is where the already mentioned __proto__ comes in. If you create a new object something like this happens: child.__proto__ = ChildConstructor.prototype; This __proto__ property is then used by the interpreter if you use properties that are "inherited" from the prototype (like .num). This makes sure the properties "child" gets from the prototype are available, even if you assign a new prototype to the constructor function after you created some objects.
1

The .prototype property has special meaning to JavaScript, but only for functions, which is why your code doesn't work.

With "protypical inheritance", you don't inherit from a base class. Instead, you start with with some object and create a new one, using the first as a prototype. Then you add properties and methods to the newly created object.

I'm not sure what you mean by "what are your methods?" Do you mean programming methodology, or something like that?

UPDATE: Your question is pretty broad, and I don't think I can do it justice with a short answer here.You might want to have a look at the SO question below, which has a good discussion of JS prototypes, as well as some links to stuff that other people have found helpful on this topic.

How does JavaScript .prototype work?

Comments

0
var parent = {
  num : 66
};
var child = {
   __proto__: parent
};

2 Comments

__proto__ is non-standard and only works in some browsers/engines. It would be nice if it was officially exposed, though.
this is sooo much simpler, why isn't this standard?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.