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.
var num = 10;
is same as
var num = new Number(10);
will have the same constructor for both the cases.
- console.log(num.constructor);
- var obj = {};
- or
- var obj = new Object;
- obj.constructor === Object;
- var obj = [];
- or
- var obj = new Array;
- obj.constructor === Array;
- var obj = 3;
- or
- var obj = new Number(3);
- obj.constructor === Number;
__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
- function Person() {
- this.name = "irshad"
- };
- var eve = new Person("Eve");
- console.log(Person.prototype);
- console.log(eve.__proto__);
- eve.address = "ald";
- console.log(eve.__proto__ == Person.prototype);
Example 2
- var Person = function() {};
- var Employee = {
- getName: function() {
- console.log('irshad');
- }
- };
- Person.prototype.__proto__ = Employee;
- var empObj = new Person();
- empObj.getName();
- console.log(Person.prototype === empObj.__proto__);
Example 3
- var Person = function() {};
- var Employee = {
- getName: function() {
- console.log('irshad');
- }
- };
- var empObj = new Person();
- empObj.__proto__ = Employee;
- empObj.getName();
- console.log(Person.prototype === empObj.__proto__);
Example 4
- function Person() {};
- Person.prototype.myname = function() {
- console.log('irshad');
- };
- var personObj = new Person();
- console.log(personObj.__proto__ === Person.prototype);
- personObj.myname();
Example 5
- function Person() {};
- Person.prototype.myname = function() {
- console.log('irshad');
- };
- var personObj = {
- __proto__: Person.prototype
- }
- console.log(personObj.__proto__ === Person.prototype);
- personObj.myname();
__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.
- var Person = {
- __noSuchMethod__: function(funcId, args)
- {
- console.log(funcId, '(' + args.join(', ') + ')');
- }
- };
- Person.getName("Mohammad", "Irshad");
-
- Person.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.