Javascript notes: deeply analyzes the creation (bottom) of objects in Javascript-from object creation to JavaScript program optimization

Source: Internet
Author: User
Tags javascript array

Why is the current mainstreamProgramAre all objects created in the language? The following definition gives us an answer:

In object-oriented languages, objects are defined as anything that people want to study. From the simplest integer to the complex aircraft, objects can be viewed as objects, it not only represents specific things, but also abstract rules, plans, or events.Aside from these abstract definitions, we can understand them from the underlying computer technology,Objects are actually a way to store data..

Writing the next article in this series is really hard for me. I originally wanted to write closures and prototypes, but I always felt bad. I told us when I was discussing JavaScript with a former colleague.In computer languages, no matter what kind of variables, data is actually stored in computers.The program is composed of data and operations. He said that since I am learning how to create a JavaScript Object, you can think about what the object is, he went on to say, in computer languages, no matter what you are creating, such as basic data types or complex data types, variables are actually variables, and variables are data in programs, the running of a program is the process of constantly retrieving the data, creating the data, then retrieving the data, repeating the loop, and finally making the data persistent. He also said, I often ask him to improve program performance, optimization program skills, thenImproving the data retrieval capability in the program is the ultimate program performance optimization..

I recently studied Yahoo's front-end technology to find out what this guy is talking about.

Yahoo's front-end technical force said:

There are four data storage methods in JavaScript (the most basic ):

Direct Volume

A direct volume only represents itself and does not store specific locations. The direct quantities in Javascript include strings, numbers, Boolean values, objects, arrays, and functions. Regular Expressions and special null and undefined expressions.

Variable

A data storage unit defined by a developer using the keyword var.

Array Element

It is stored in the Javascript array object and indexed by numbers.

Object Member

Stored in a JavaScript Object and indexed by string.

Yahoo's front-end engineers tested the performance of these data storage methods. In most browsers, the speed of reading the direct volume and variables is much higher than that of reading array elements and object members. Maybe we can think about object creation like this:Every time we create an object, we increase the complexity of data retrieval. If an object is nested, the performance loss will increase by a geometric multiple..

The creation of an object may be the beginning of the slow application response nightmare..

As I have said before, JavaScript can also use the phrase in Java: Everything is an object, and even a function can be used as an object. In this case, creating a function is to create an object, and how to create this object is the key to improving the performance of the program.

It's a lot of knowledge to create a function object in Javascript. If we treat a function as an object, some concepts that are hard to understand will be much better understood.

In this article, I talked about the scope. What is the scope?The scope is actually an internal attribute [Scope] of the function object instance ]. When a function is created, the internal attribute [Scope] contains a set of all objects in the scope of the function. This set is called the scope chain. The scope chain determines the data that the function can access..

Next I will create a function add,CodeAs follows:

<SCRIPT type = "text/JavaScript">
FunctionAdd (A, B)
{
VaRSum = A + B;
ReturnSUM;
}
</SCRIPT>

This is a very common function. We define this function first, and remember not to execute it first. When the page is loaded, this function will also be initialized, the defined function belongs to window and is pre-compiled when the page is loaded. The scope chain of function add is as follows:

When the function add is created, for example, the objects contained in the scope chain in the Add function. (Only partially)

I can't tell you why. In firebug, I defined the breakpoint at function Add. You can see the following results:

This is the scope chain of function add, which is the set of objects that can be accessed by this function.

Only one function is defined here. The function can only see its nature at runtime. See the following code:

<SCRIPT type = "text/JavaScript">
FunctionAdd (A, B)
{
VaRSum = A + B;
ReturnSUM;
}
Add (1, 2 );
</SCRIPT>

When a function is executed, the execution function creates an internal object called "execution context. This is the execution environment I mentioned in this Article. However, it is an internal object created during function execution. It is not an inherent attribute of a function, A function is created only when it is executed. A runtime context defines the stage in which a function is executed. The runtime environment corresponding to each function execution is unique. Therefore, multiple calls to the same function may create multiple runtime contexts. When the function execution is complete, the executor context is destroyed.

My understanding of the runtime context is an anonymous function in the function. The reason is that when the executor context is created, the corresponding scope chain will also be created, because I think only functions in Javascript have the attribute of the scope chain, but this special scope chain has a special role: This scope chain is used to parse the identifier..

The content of my object has been finished. By creating an object, I have introduced the concept of scope and execution environment, it is also the creation of special function objects in object creation. I introduced the essence of scope and execution environment, and the content of object creation is complete, in the next blog, I will talk about how to write efficient javascript programs through the scope chain and execution environment.

I haven't written a blog for a long time. Some friends said that I can't do it anymore. Of course not. There have been so many things recently. The key has been disconnected for more than 20 days. It was good yesterday. I want to be a technical expert. I think any skill improvement must be discussed with everyone. So I will stick to it, but I can't do it without time.

If you like software, you should be a hobby. No matter what you insist on, you really like it.

 

 

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.