SoFunction
Updated on 2025-04-14

JavaScript prototype, execution, context, closing

To learn JavaScript well, there are several basic concepts that must be understood: prototype, execution, context, and closure.
Prototype

In JavaScript language, Prototype is usually used to implement OO. Here, we will not discuss the OO implementation of JavaScript too much, and focus on the memory model of objects in JS. Before you start, you need to clarify the following points:
1. In JS, the following data types exist: string, number, boolean, object, function (note: the first letters are all lowercase).
2 Built-in data types such as "Object", "String", "Date" are actually function names in JS (using "alert(typeof Object)" can be verified, and the output is "function"). The data type we usually refer to is actually an object generated through "new Date".
3. In JavaScript, objects are all associated array (hash table), which can dynamically specify the object's property.
4. In Firefox, you can use the "__proto__" attribute to view the "prototype" of an object.

Let's take a look at a simple example:

function Person() { = 10; = "test";} = new Object;var p = new Person;alert(p); // output "[object Object]"alert(p.__proto__); // output "[object Object]"

It can be seen that the Person data type has a "prototype". If you change this prototype, it will affect all objects of Person type that have been generated, and it will also affect objects of Person type that will be created in the future. If a function's prototype property is specified, all object instances generated using the function (using the new operator) have the prototype. In Firefox, it can be accessed using the "__proto__" property.

Generally speaking, we say that objects in JS inherit Object data types. How does this reflect it? Let's modify the above program slightly:

function Person() { = 10; = "test";}var p = new Person;alert(p); // output "[object Object]"alert(p.__proto__); // output "[object Object]"alert(p.__proto__.__proto__); // output "[object Object]"alert(p.__proto__.__proto__ == ); // output "true"alert(p.__proto__.__proto__.__proto__); // output "null"

As can be seen from the above program, Person's "prototype" (p.__proto__.__proto__) is exactly the end point of the prototype chain (its own ancestor is null).

In JS, Object is a function, and at the same time, all instances of functions are Objects. Please see the following program:

/* Object, Function are both function data types */alert(typeof Object); // output "function"alert(typeof Function); // output "function"/* The prototype of the function is an empty function */alert(); // output "function() {}"alert(Function.__proto__ == ); // output "true"/* function is object, and the end point of its prototype chain is */alert(Function.__proto__.__proto__ == ); // output "true"/* function is object, and the end point of its prototype chain is */alert(Function.__proto__.__proto__ == ); //output "true"/* Object is an instance of a function */ alert(Object.__proto__ == ); // output "true"alert(Object.__proto__.__proto__ == ); // output "true" changes will affect "Object", and the changes will affect all instances of a function.