Javascript has function and object structure. We tries to understand this.
Function is an object and an object is created by some function.
When a function creates an object, the function assign its prototype value to __proto__ property of newly created object.
Object.prototype is the last object in prototype chain. It is base object for all functions and objects.
Javascript has pattern that __proto__ of function would be type of function and __proto__ of object would be type of object. This is also true for Number, Array etc.
Lets check diagram of javascript function and object structure.
(We shall call in-built unnamed function as System function.)
Function is an object and an object is created by some function.
When a function creates an object, the function assign its prototype value to __proto__ property of newly created object.
Object.prototype is the last object in prototype chain. It is base object for all functions and objects.
Javascript has pattern that __proto__ of function would be type of function and __proto__ of object would be type of object. This is also true for Number, Array etc.
Lets check diagram of javascript function and object structure.
(We shall call in-built unnamed function as System function.)
If we create obj using object literal,
var obj = { a: 0 };
The obj is created by Object. Here, the Object function is used implicitly as creator function.
obj.__proto__ === Object.prototype // true
If we create an instance using creator function.
var foo = new Foo();
The foo is created by Foo.
foo__proto__ === Foo.prototype // true
Now, Foo also is an object (functional-object). Who creates Foo?
Foo is created by Function.
When we write like this.
var Foo = function () {
};
The Function acts as creator function and creates Foo object.
Foo.__proto__ === Function.prototype // true
Now, who creates Function? Before asking the question lets ask who creates Object?
The Object is a creator function to create other objects. So it is also created by Function.
Object.__proto__ === Function.prototype // true
Ok. now who creates Function?
Actually Function creates itself!!!
This seems weird and it is actually. Javascript tries to emerge one pattern. It is not right internally though that Function creates itself.
Function.__proto__ === Function.prototype // true
If you look closely the above diagram you will see that.
Foo.__proto__ === Function.__proto__ === Object.__proto__ //true
Here, Foo, Function and Object all are functions and functions always have __proto__ of type function.
So if we do
console.log(Function.__proto__)
We would see something like this
function () { [native code] }
This is inbuilt unnamed function (System function) what is assigned to __proto__ of any of type functional-object.
Now, who creates System function?
Function.__proto__.__proto__ === Object.prototype // true
This is strange. System function (Function.__proto__) is created by Object and Object is created by Function. This is a cyclic referencing. Something is first and something is last. Javascript assign either Function.__proto__.__proto__ or Object.__proto__ later to mimic the cycle.
This design is to make Object.prototype last object in prototype chain.
But, here System function is a type of function and its __proto__ should be a type of function not a type of object. But, the function's __proto__ is Object.prototype.
But, anyways some function has to have Object.prototype as __proto__ of itself at the end. Because eventually all needs to end at Object.prototype.
Ok... Who created Object.prototype? The object's __proto__ is null. So it seems no one creates it.
Object.prototype.__proto__ // null
Same way, does System function create anything? The function's prototype is undefined. So it seems it doesn't create anything.
Comments
Post a Comment