Javascript prototype, executing, context, Closure

Source: Internet
Author: User

To learn JavaScript well, you must understand several basic concepts: Prototype, executing, context, and closure.
Prototype

In JavaScript, prototype is usually used to implement oo. Here, we will not discuss too much about the Javascript oo implementation, focusing on the memory model of objects in Js. Before you start, you need to clarify the following points:
1. js contains the following data types: String, number, Boolean, object, and function (note: the first letter is lowercase ).
2 built-in data types such as "object", "string", and "date" can be verified by using the function name (using "alert (typeof object)" In Js, output as "function "). We usually refer to the Data Type of "date", which is actually an object generated by "new date.
3. In JavaScript, all objects are associative array (hash table). You can dynamically specify the property of an object.
4. You can use the "_ PROTO _" attribute in Firefox to view the "prototype" of an object ".

Here is a simple example:

Function person () {This. age = 10; this. name = "test";} person. prototype = new object; var P = new person; alert (p); // output "[object]" alert (P. _ PROTO _); // output "[object]"

It can be seen that the person data type has a "prototype". If you change this prototype, it will affect all the generated person objects, and will also affect the objects of the person type created later. If the prototype attribute of a function is specified, all object instances generated using the function (using the new operator) have the prototype. In Firefox, you can use the "_ PROTO _" attribute for access.

In general, how does one reflect that all objects in JS inherit the object data type? WeProgramSlightly modify:

Function person () {This. age = 10; this. name = "test";} var P = new person; alert (p); // output "[object]" alert (P. _ PROTO _); // output "[object]" alert (P. _ PROTO __. _ PROTO _); // output "[object]" alert (P. _ PROTO __. _ PROTO _ = object. prototype); // output "true" alert (P. _ PROTO __. _ PROTO __. _ PROTO _); // output "null"

From the above program, we can see that the "prototype" of person is not explicitly specified here. prototype, but uses the default value) "prototype" (P. _ PROTO __. _ PROTO _) is exactly the object. prototype, object. prototype is the end of prototype chain (its ancestor is null ).

In JS, objects are functions, and all function instances are also objects. See the following program:

/* objects and functions are function data types */Alert (typeof object); // output "function" alert (typeof function ); // output "function"/* function prototype is an empty function */Alert (function. prototype); // output "function () {}" alert (function. _ PROTO _ = function. prototype); // output "true"/* function is an object, and its prototype chain ends with an object. prototype */Alert (function. _ PROTO __. _ PROTO _ = object. prototype); // outpu T "true"/* the object is a function instance */Alert (object. _ PROTO _ = function. prototype); // output "true" alert (object. _ PROTO __. _ PROTO _ = object. prototype); // output "true" to change the function. prototype affects "object" and changes object. prototype affects all function instances.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.