JavaScript prototype,executing,context,closure_prototype

Source: Internet
Author: User
Tags object object
To learn JavaScript well, there are several basic concepts to be clear: prototype,executing,context,closure.
Prototype

In the JavaScript language, you typically use prototype to implement OO. Here, we do not too much discussion of the OO implementation of JavaScript, focus on the memory model of the object in JS. Before you begin, you need to make the following points clear:
1. js, there are several types of data: string,number,boolean,object,function (note: The first letter is lowercase).
2 "Object", "String", "Date" and other built-in data types, in JS is actually the function name (using "alert (typeof Object)" Can be validated, the output is "function"). We usually refer to the type "date" of the data type, which is actually the object generated by "new Date".
3. In JavaScript, objects are associative array (hash table) that can dynamically specify the property of an object.
4. In Firefox, you can use the "__proto__" property to view the "prototype" of an object.

Let's take a look at a simple example:

function person () {this.age = THIS.name = "Test";} Person.prototype = new Object;var p = new Person;alert (p); Output "[Object Object]" alert (p.__proto__); Output "[Object Object]"

You can see that the person data type has a "prototype", and if you change this prototype, it affects all objects of the person type that have been generated, and also affects the objects of the person type that you established later. If you specify the prototype property of a function, all instances of the object that are generated using the function (using the new operator) have the prototype, which can be accessed using the "__proto__" attribute in Firefox.

In general, we say that the object in JS inherits the object data type, how is this manifested? We modify the above procedure slightly:

function person () {this.age = 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, the person's "prototype" (here, not explicitly specify Person.prototype, but use the default value) of the "prototype" (P.__PROTO__.__PROTO__) It is object.prototype that Object.prototype is the end of prototype chain (its own ancestor is null).

In JS, object is a function, and all instances of function are also object. Please see the following procedure:

/* object, function is a function data type */alert (typeof object); Output "function" alert (typeof function); The prototype of the output "function"/* function is an empty function */alert (function.prototype); Output "function () {}" alert (function.__proto__ = = Function.prototype); Output "true"/Function is object and its prototype chain endpoint 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" Changes Function.prototype affects "Object", and changing object.prototype affects all instances of the function.
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.