-2

I was trying to create object different way and trying to observe prototype property of the object created in each way.

var a = {a: 1}; 
//The prototype chain as mentioned in [here][1]
// a ---> Object.prototype ---> null

Now, i have observed that a.prototype is undefined. Question1:If it is undefined then how the prototype chain is formed? a.hasOwnProperty exist because of prototype chain.

Now if i say:

var myFunc = function(){};
// Prototype chain: myFunc ---> Function.prototype ---> Object.prototype ---> null

for this way of creation, myFunc.prototype exist.

Question2: Why this difference exist?

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Ratnesh Lal
  • 401
  • 4
  • 8
  • 18
  • 1
    `prototype` is a special property that only functions have. – Felix Kling Feb 18 '14 at 07:56
  • Constructor functions have prototype, you can use Object.create to create an (empty or second argument) object with the passed object (first argument) as the first prototype used to look up the prototype chain. More on how this works here: http://stackoverflow.com/a/16063711/1641941 – HMR Feb 18 '14 at 12:20

1 Answers1

2

The prototype attribute isn't the object's prototype. That's (usually) the __proto__ attribute, although I don't think any version of the ECMAScript standard requires that attribute. The prototype attribute of a function Foo is the object that will be used as the prototype for an object generated by new Foo().

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • While `.__proto__` is deprecated in most browsers, I believe that ES6 is adding full support for the property, as a magical (mutable) getter/setter. – Norguard Feb 18 '14 at 08:11