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 too much about the OO implementation of JavaScript, but focus on the memory model of objects in JS. Before starting, you need to clarify the following points:
1. In JS, there are the following data types: string, number, boolean, object, function (note: the first letters are all lowercase).
2 "Object", "String", "Date" and other built-in data types are actually function names in JS (use "alert(typeof Object)" to verify, the output is "function"). The data type we usually refer to as "Date" is actually an object generated by "new Date".
3. In JavaScript, objects are all associative arrays (hash tables), and the properties of objects can be dynamically specified.
4. You can use the "__proto__" attribute in Firefox to view the "prototype" of an object.
Let’s look at a simple example:
function Person() { this.age = 10; this.name = "test";}Person.prototype = 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 the Person type objects that have been generated, and it will also affect the Person type objects created in the future. If you specify the prototype attribute of a function, all object instances generated using the function (using the new operator) will have the prototype. In Firefox, it can be accessed using the "__proto__" attribute.
Normally, we say that objects in JS inherit the Object data type. How is this reflected? Let's slightly modify the above program:
function Person() { this.age = 10; this.name = "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__ == Object. prototype); // output "true" alert(p.__proto__.__proto__.__proto__); // output "null"
As you can see from the above program, Person's "prototype" (here, there is no clear Specify Person.prototype, but use the default value) The "prototype" (p.__proto__.__proto__) is exactly Object.prototype, Object.prototype is the end of the prototype chain (its own ancestor is null).
In JS, Object is function, and all instances of function are also Object. Please look at the following program:
/* Object and Function are both function data types*/alert(typeof Object); // output "function"alert(typeof Function); // output "function"/* Function The prototype is an empty function */alert(Function.prototype); // output "function() {}"alert(Function.__proto__ == Function.prototype); // output "true"/* function is object, whose The end point of the prototype chain is Object.prototype */alert(Function.__proto__.__proto__ == Object.prototype); //output "true"/* Object is an instance of function*/ alert(Object.__proto__ == Function.prototype) ; // output "true" alert(Object.__proto__.__proto__ == Object.prototype); // output "true" Changing Function.prototype will affect "Object", and changing Object.prototype will affect all instances of function.