Properties Of Object.prototype In JavaScript

Introduction

 
In JavaScript, everything is an Object and all the defined objects are descended from Object.
 
Object.prototype is used to inherit properties and methods from Object to the Constructor method. It may be overridden. We can use any constructor to create the object of another type with the help of the Object.create() method and assign it to the new constructor prototype.
 
Eg 
 
newConstructor.protype = Object.create(myConstructor.prototype)
 
In the above, we are creating a ‘newConstructor’ with the help of already defined constructor  ‘myConstructor’.
 
You can check my previous article for Object and Function.
 

Properties of Object.prototype

 
constructor
 
It specifies the function that creates the object prototype. Basically, all the objects have a constructor property. Objects created without the explicit use of a constructor function (like the simple assignment of numbers, strings, arrays etc.) will have a constructor property that points to the constructor type for that object. 
 
 
Eg: The statement
 
var num =  10;
is same as
var num = new Number(10);
 
will have the same constructor for both the cases.
  1. console.log(num.constructor); //    function Number() { [native code] }    
  2. var obj = {};  
  3. or  
  4. var obj = new Object;  
  5. obj.constructor === Object; // true    
  6. var obj = [];  
  7. or  
  8. var obj = new Array;  
  9. obj.constructor === Array; // true    
  10. var obj = 3;  
  11. or  
  12. var obj = new Number(3);  
  13. obj.constructor === Number; // true  
__proto__
 
This property of Object.prototype is  a type of accessor property that exposes the internal prototype through which it is accessed. 
 
__ proto __ is used by instances we created to access the prototype of its constructor. 
 
In the below examples we can see how we can use __proto__ in different cases.
 
Example 1
  1. function Person() {    
  2.     this.name = "irshad"    
  3. };    
  4. var eve = new Person("Eve");    
  5. console.log(Person.prototype);    
  6. console.log(eve.__proto__); //it will print the same as above    
  7. eve.address = "ald";    
  8. console.log(eve.__proto__ == Person.prototype); //true   
Example 2
  1. var Person = function() {};    
  2. var Employee = {    
  3.     getName: function() {    
  4.         console.log('irshad');    
  5.     }    
  6. };    
  7. Person.prototype.__proto__ = Employee;    
  8. var empObj = new Person();    
  9. empObj.getName(); // irshad    
  10. console.log(Person.prototype === empObj.__proto__); // true  
Example 3
  1. var Person = function() {};    
  2. var Employee = {    
  3.     getName: function() {    
  4.         console.log('irshad');    
  5.     }    
  6. };    
  7. var empObj = new Person();    
  8. empObj.__proto__ = Employee;    
  9. empObj.getName(); // irshad    
  10. console.log(Person.prototype === empObj.__proto__); // false  
Example 4
  1. function Person() {};    
  2. Person.prototype.myname = function() {    
  3.     console.log('irshad');    
  4. };    
  5. var personObj = new Person();    
  6. console.log(personObj.__proto__ === Person.prototype); // true    
  7. personObj.myname(); // irshad  
Example 5
  1. function Person() {};    
  2. Person.prototype.myname = function() {    
  3.     console.log('irshad');    
  4. };    
  5. var personObj = {    
  6.     __proto__: Person.prototype    
  7. }    
  8. console.log(personObj.__proto__ === Person.prototype); // true    
  9. personObj.myname(); // irshad  
__noSuchMethod__
 
This property has been used to reference a function that is executed when it is not existed on the object. But this prototype property to perform such functionality has been removed and is no longer available.
  1. var Person = {    
  2.     __noSuchMethod__: function(funcId, args)    
  3.   {    
  4.         console.log(funcId, '(' + args.join(', ') + ')');    
  5.     }    
  6. };    
  7. Person.getName("Mohammad""Irshad");    
  8. // getName, Mohammad, Irshad    
  9. Person.getAddress("281/178""Lukerganj""Allahabad""India");    
  10. // getAddress, 281/178, Lukerganj, Allahabad, India   
In the above example, it will execute the __noSuchMethod__ if we try to execute any method that is not available.
 

Summary

 
You can not try the above example as this functionality is no longer available with object prototype properties.