Skip to main content

Javascript function and object structure

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.)


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

Popular posts from this blog

Basics of quantum computing: change of basis

Computing requires classical bits, which can hold a value of 0 or 1. Quantum computing gets benefit from superposition of quantum bit (qubit). Qubit is a representation of state of quantum object (electron, proton, photon, etc). The qubit holds a value between 0 and 1 while it is in superposition state. Superposition means the qubit possesses multiple values at the same time so we can harness its power for parallel computing. Once we try to measure the value it can be collapsed into either 0 or 1 based on probability. (Observer collapses wave function simply by observing.) Using bloch sphere, we can represent qubit physically. (The bloch sphere is a physical model to represent a spin state of qubit) A state vector can point to any direction from the center of the sphere. If a vector points to Z+ and Z-, it represents 0 and 1 state respectively. All other vectors represent superposition state in a Z basis. Now, if a vector points to X+, X-, Y+, Y-, all are at a 90-degree angle from ...

A deep understanding of neural network

Basics Neural network is a mathematical function to solve some programming problems which is almost impossible to be solved with conventional programming. Neural network consist of neurons and weights. Neurons are made up with activation function and holds output of the function called activation value. Weights are just values which connects one neuron to another. Neurons are grouped and forms a layer. Each layer is connected with other layer by weights. There are three kind of layers; input layer, hidden layer and output layer. Any neural network contains at least one hidden layer, though there might be more than one hidden layers are possible. Input layers accepts input, calculate activation values for next hidden layer, the next layer does same process and pass result to next of it. This goes on until output layer is reached.  Each layer calculates Z values using weight and activation. Using Z values activation of next layer is calculated. Once we get activation of output laye...

`this` keyword in javascript

this keyword used to reference current object in javascript. There are two scenario where current object is determined differently. * Calling object represented by `this` in function. * Arrow function can reference object where the function defined. To demonstrate the above scenarios we shall take some examples. Suppose, var x = 10; (function() {    console.log(this.x); })() // 10 (() => {    console.log(this.x); })() // 10 There are two self executing functions both resulted in "10". At first look it seems behaving same, but there is basic deference what we going to see. const xObj = {     value: 10,     getValue: () => {         return this.value;     } }; const yObj = {     value: 10,     getValue: function () {         return this.value;     } }; xObj.getValue(); // undefined yObj.getValue(); // 10 Why undefined for `xO...