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

Monad: in programming

Monad is a structure with type constructor M and two functions unit ( η) and multiplication ( μ). and   To lift value from a to Ma we need unit function and to flatten from M(Ma) to Ma we need multiplication . Why do we need monad in functional programming? Why do we need functional programming anyways? Functional programming is a paradigm which uses pure functions to build an application. The power of pure functions are, we can compose them and we can build complex things out of the small and simple functions. If we have these functions   and   We can get   We can use below general purpose compose function to compose any two pure functions.   If we need to compose three functions then we can do like Now, we can't build application using only pure functions. It is next to impossible. We need side effects anyways to make the application practical in use. To achieve the goal, we need to keep pure functions in center/core part of application and need to move side effects outsid

Monad: to programming from category theory

We have seen the summarized article "monad in programming" in the previous post. Let's find out what is a monad in category theory and how the concept became relevant for functional programming. First, we need to understand the basics of category theory. Category A category consists of objects and morphisms (relationships) between those objects. More of it, the morphisms should be composable too. Below is an example of a category. Each dot is an object and each arrow shows morphism from some object to another. As arrows are composable, if there is a morphism from object a to object b , and there is another morphism from b to c , then we can find a morphism from a to c .  If you notice, there is identity morphism too, it exists for each object, but we have shown it just for x . It is a morphism from an object to itself. If we compose identity morphism with any other morphism w , we get the same w in return. Functor A functor is a relationship between categories