Skip to content

Instantly share code, notes, and snippets.

@victorwhy
Last active August 29, 2015 14:06
Show Gist options
  • Save victorwhy/5e6f8c92ddf5357b100d to your computer and use it in GitHub Desktop.
Save victorwhy/5e6f8c92ddf5357b100d to your computer and use it in GitHub Desktop.
__proto__ vs prototype

The proto property of Object.prototype is an accessor property (a getter function and a setter function) that exposes the internal [[Prototype]] (either an object or null) of the object through which it is accessed.

FYI MDN says proto is being deprecated - use Object.getPrototypeOf and Object.setPrototypeOf (setter not recommended to use) instead.

Basically, when a new instance of a constructor is created, it's proto is the same as the constructor's prototype.

// Declare a function to be used as a constructor
function Employee() {
    /* initialise instance */
}

// Create a new instance of Employee
var fred = new Employee();

// Test equivalence
fred.__proto__ === Employee.prototype;

Let's look at a more comprehensive example:

Test Code to see what it's like:

Object.O1='';
Object.prototype.Op1='';

Function.F1='';
Function.prototype.FP1='';

Cat = function(){};
Cat.C1='';
Cat.prototype.Cp1='';

mycat = new Cat();
o= {}

console.log(mycat);
console.log(mycat.__proto__);
console.log(o);
console.log(o.__proto__);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment