1

I've just opened Chrome Dev Tools and typed this code:

function Car() { 
    this.value = 10; 
}
var cc = new Car();

But when I typed this code:

cc.prototype

I got undefined. Why?
As I know 'cc' object must have the same prototype as Car constructor.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
malcoauri
  • 11,904
  • 28
  • 82
  • 137
  • Possible duplicate of [\_\_proto\_\_ VS. prototype in JavaScript](http://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript) – nicael Dec 20 '15 at 11:59
  • And why would you access the prototype of the new instance, are you sure you don't want `Car.prototype` – adeneo Dec 20 '15 at 11:59

2 Answers2

0

You need to use cc.__proto__ to get the prototype object.

See https://stackoverflow.com/a/9959753/4466618 for the difference.

Community
  • 1
  • 1
aarjithn
  • 1,151
  • 8
  • 20
0

The prototype is inside the cc.__proto__ attribute.

The browser (JS engine) place it there.

The new function looks something like:

function new() {
   var newObj = {},
   fn = this,
   args = arguments,
   fnReturn;

   // Setup the internal prototype reference
   newObj.__proto__ = fn.prototype;

   // Run the constructor with the passed arguments
   fnReturn = fn.apply(newObj, args);

   if (typeof fnReturn === 'object') {
       return fnReturn;
   }

   return newObj;
}

This line is the relevant line for you:

newObj.__proto__ = fn.prototype;

The Object.prototype

The Car.prototype is the definition of all the methods and properties of the Car object. The cc instance store it inits __proto__

CodeWizard
  • 128,036
  • 21
  • 144
  • 167