0

I'm new to web-development, coming from C/C++/x86. For all my efforts to circumvent it, it appears as though I'll need to use Javascript (surprise!).

Well, if I'm going to have to use it, I might as well understand whats going on. This has been difficult, since I'm used to being able to easily map C/C++ down to x86/x86_64 to figure out what's happening. Not to mention, Javascript is an entirely different paradigm to get accustomed to.

So, what is this prototype keyword, and how is this prototypical inheritance implemented?

After reading a few highly touted books and guides, I'll I've seen is examples. Every object (save the global object) has a prototype, which is another object. There has to be a very simple mechanism operating in the background on how these all these objects are linked.

I don't need to hear any more of "a new object inherits the properties of an old object" wishy-wash stuff you tell 12 year olds. For example, I know how C++'s virtual table works to understand how virtual functions work - everything makes perfect sense.

So, how does does Javascript use prototyping?

Thank you.

gone
  • 2,587
  • 6
  • 25
  • 32
  • So read the V8 source. And bear in mind not all implementations will implement it the same way. So far it sounds like you're over-thinking it. – Dave Newton Jul 12 '13 at 22:25
  • http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work – dsgriffin Jul 12 '13 at 22:26
  • @DaveNewton I can't help it. Not knowing what's going on in the background doesn't seem.. right. – gone Jul 12 '13 at 22:28
  • @gone That's fine, if you have the time. But every engine will implement it in its own way. The *behavior* is what's important. Another option is to implement your own JavaScript as a learning exercise; the bulk of it is straight forward. – Dave Newton Jul 12 '13 at 22:33
  • I'd say go read the ECMAScript draft. It's what all the JavaScript VM's are written based off. http://people.mozilla.org/~jorendorff/es6-draft.html#sec-4.3.5 , http://people.mozilla.org/~jorendorff/es6-draft.html#sec-4.2.1 – travis Jul 12 '13 at 22:34
  • The "manual": http://es5.github.io/, http://www.w3.org/TR/WebIDL/#interface-prototype-object – Travis J Jul 12 '13 at 22:34
  • 1
    This is one of those questions where everyone gives you the most bullshit answer to. Prototype is just a way to add a property to all future and current instances of a type. Plain and simple. – The Muffin Man Jul 12 '13 at 22:39
  • @Nick Which says nothing about the implementation, which seems to be what he's asking. – Dave Newton Jul 12 '13 at 22:48

2 Answers2

0

Think of prototype properties as static. prototype is just an access word, it means nothing except assign properties to, or write over, the Object you are accessing with the word prototype.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

prototype is a language construct of Javascript. It's JavaScript's approach to inheritance.

In javascript (until ECMAScript6 but that's a different subject) there are not true classes. Instead we create instances of objects, and we can create child instances of those objects using the new keyword. For instance:

var parent = function() {};
parent.prototype.lastName = "Flynn";
var child = new parent();
console.log(child.lastName); //Outputs "Flynn"

Also, interestingly:

parent.prototype.nationality = "American";
console.log(child.nationality); //Outputs "American"
Casey Flynn
  • 13,654
  • 23
  • 103
  • 194