3

Possible Duplicate:
How does JavaScript .prototype work?

Coming from Java background, I'm trying to understand javascript.

Please let me know if these are right.

  1. Like in java, there is a supreme Object, from which all other objects inherit.
  2. The prototype property is like a pointer that points to the parent object (classes in java)
  3. For "Object" object, the prototype is null.
  4. prototype property's values are strings denoting the objects nomenclature and aren't pointers like in C. The pointer concept is implemented using the hidden attribute,[[PROTOTYPE]] that is not accessible in script.

I'm using node.js instead of browser to learn JS. I tried,

var human = Object.create(null);  // same as var human;
console.log(Object.getPrototypeOf(human)); //null

var man  = Object.create(human);
console.log(Object.getPrototypeOf(man));
//{}
//expected 'human'

var person = Object.create(Object.prototype); // same as var person = {}
console.log(Object.getPrototypeOf(person));
//{}
//expected 'object'
0

3 Answers 3

0
  1. Yes, with the exception of Object.create(null) which doesn't inherit from anything.
  2. Not sure what "parent object" is, but the prototype of a function consists of functions that are accessible by instances. Because Object.prototype.toString exists, so does {}.toString - it is inherited.
  3. Yes, the prototype chain ends here.
  4. No, .prototype (or the value returned by Object.getPrototypeOf) is an object which you can extend with properties, so that those properties are inherited by instances.

Your examples:

var human = Object.create(null);  // same as var human;

No, it creates an empty object that doesn't inherit from anything. var human; sets human to undefined - which is not an object but a primitive value (not everything is an object in JavaScript).

var man  = Object.create(human);
console.log(Object.getPrototypeOf(man));
//{}
//expected 'human'

Object.getPrototypeOf(man) returns the object human. This is an empty object; node shows it as {}. It is not a string. In fact, objects can be assigned to multiple variables. An object does not have a variable bound to it, so by design it is not possible at all to get a string. To check equality, you could do Object.getPrototypeOf(man) === human which yields true.

var person = Object.create(Object.prototype); // same as var person = {}
console.log(Object.getPrototypeOf(person));
//{}
//expected 'object'

This is indeed the same as {} - an empty object that inherits directly from Object. As said above, the prototype is Object.prototype and not a string. It looks empty but that's because Object.prototype's functions are available by default and hidden.

0

First, you need to read about the create method before you attempt to use it.

Second, invoke objects in the following manner:

var human = {}, man = {}, person = {};

Third, this is not Java. In Java classes and an understanding of classes are pretty important. Prototypes are completely optional in JavaScript. The only performance benefit offered by prototypes comes when you code makes heavy reuse of functions by reference in different scopes. If this style of coding does not apply to you then you likely do not need to use prototypes.

Prototypes are inherited from the parent, or some ancestor, object. A JavaScript parser will first look for local assignments and if none exist will then examine the current object's prototype for the presence of the named reference. In a nutshell this all you need to know at this early point in writing JavaScript.

0

Firstly, IMO, don't start off with node. Node is going to drag you into a pool of quicksand if you don't know javascript yet. Secondly, when I started off with function based code, then OO and only then I moved to prototypes. But if you really want to know, this question has already been answered over here. Feel free to ask me if you have any more questions!

2
  • if not node, what do you suggest i use?
    – John Eipe
    Jul 27, 2012 at 19:02
  • Just use regular js. Node is primarily meant for server side javascript, something that is a whole nother ballpark. See w3schools for tutorials
    – Kpower
    Jul 28, 2012 at 0:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.