JavaScript Advanced Features

Source: Internet
Author: User
Tags closure

The advanced feature of JavaScript is that learning JavaScript must be understood, otherwise we can learn JavaScript in the future with a variety of special effects and transformations that are really the same as the heavenly book. Some notes on some of the advanced features of JavaScript are shared with you today. The following content is my understanding of these features, literary limited, so we forgive, if there is wrong place, hope treatise. So let's get started!

The advanced features of JavaScript include objects, closures, prototypes, and inheritance. Then I'll explain it all.

(i) object

We all know that in JavaScript, everything is an object. So we often say that the function is also an object, in ECMAScript, we call it a function, but in JavaScript we will call him object, but the word is different, the essence is the same. So in ECMAScript we define functions in a way that is similar to the way we define objects in JavaScript.

(1) Defining objects

1.var Object Name =new objects ();

This definition is rarely used, because in this way we can assign JavaScript's built-in object to a new object, but when we use the built-in object, we call the object name directly, so the definition is a bit verbose.

2.var Object Name ={key:value,key:value,key:value}

This way of defining is very much like JSON passing in the form of data, through key-value pairs to our object to add properties and methods. This way defines an object that we call a normal object. This is a common way of defining.

3. Function object Name () {}

We call this a function object, which is called a function constructor. This function has two meanings: 1. It is itself a function object and can be called as a function. 2. He can act as a benchmark function to construct other objects that we need. This definition is the most common, in this way we can construct many intrinsic function objects, the code will have a better structure.

(2) Reference object

Now that we have defined the object, we also have to grasp the reference to the object and the properties and methods that exist in the object. Because of the way they are defined, the way you reference objects is different. And the same thing is that if we write this,

(object name. Attribute); Is the invocation of the object, which is the general invocation.

First of all, the simple, the call to the ordinary object, the way is (the object name. property); Then the call to the function object is somewhat more complex, it's called (Var a=new object name (); a. property), and the two sentences are called to complete the function object.

Var a=new object name () The meaning of this sentence is that we assign a function object to a normal object, at which point we pass an address, an address to the function object. So where is this address stored? This is what new is all about, and new's role is to re-create a new space for us, and this space is to put our address. The function object then establishes a connection to the normal object. In fact, here, there is a point where the function object and the normal object are established by the return value of this. As for this, we take the time to understand, but we remember that this refers to the content that invokes this function.

A. Attributes; Because they have established a connection. So our call to a is the content and method of the function object. This completes the call ... (Ay ... It seems to be more complicated than that.)

(3) Operation of the object

There are four ways to manipulate objects: 1. Add, 2. Modify, 3. Delete, 4. Call

The call has been described in detail earlier, and there is no more talk. Talk about the others;

1. Add

For normal objects

The name of the object. New attribute = new attribute value;

The name of the object. New method =function ();

for function objects;

Normal function object. New attribute = new attribute value;

Normal function object. method =function ();

The normal function object here is the one we defined earlier.

2. Modifications

For normal objects

The object name. Property = new Property

The name of the object. Method = new function ();

For function objects

Normal function object. property = new attribute value;

Normal Function object. method = new Functions;

3. Delete

For normal objects

Delete Object name. Property = property value;

Delete Object name. Method =function ();

For function objects

Delete Normal Function object. property = property;

Delete Normal function object. method =function ();

(ii) Closure characteristics

What we often say about closures is simply that the function can use variables outside of the function.

I would like to say a few concepts, global domain: That is, from <script> start to </script> end, the content is the global domain, you can see that the function N () can call other variables a, B.

So what is the condition of the closure? Closure formation must meet 1. In the global domain you can call an intrinsic function n ();

2. The inner function n () can access the local variable B;

3. The variables that are accessed within the layer must be defined in the previous layer function

Form closure Characteristics:

Failure to form closure characteristics;

Advantages of closures:

1. Low coupling between code. How to understand,,, that is to say, we use closures to write programs, once the mainstream framework is eliminated, then our program has little impact.

2. Local variables can be shared. Through closures, we can all call the program, without knowing the composition of his execution.

3. Increase the safety level. The program in the closure is not visible, which greatly improves security.

Limitations of closures:

1. When calling a function, the last function must be called before it can be used;

2. The initialization of the inner layer function must be in the upper function.

Closures are a very important idea, so I suggest we use the closure feature when we write the program, so our program will look very tall!

(iii) prototype characteristics

The so-called prototype, plainly speaking is a property of the object itself, because once the object is created, then he must have the properties of the prototype.

I'm clear, the prototype is actually a new memory space, it's part of the function object, but it's not the same as the space we created by invoking the object, they are two different spaces, and then there is new, and our new space is independent, so that the three spaces are independent, They are connected to each other by their addresses.

The role of prototypes:

Using prototypes, we can add properties and methods to objects, and the properties and methods that are added through prototypes are public, meaning that each invocation function can be used.

As an example:

With the above examples and comments, you should be aware of how prototypes add properties and methods, and how to invoke properties and methods. What I'm trying to say is why we're going to be new to our function. Like, in the previous article, a call to a function object must precede it with new, creating a normal object to accept the function object, so we must add new to the function object.

When using prototypes, we must know a few questions:

1. The property names and methods we add are the same as the property names and methods of the object itself, and the result of the function is subject to its own properties and methods, but at this point the properties and methods we add are still in the object and do not disappear, that is, we can increase the past by adding attributes to the prototype.

2. Sequencing issues

The main thing is to increase the order of properties and new common objects;

(1) Circumstances unrelated to the sequence:

Example: But the position is different, but it does not affect the output result.

(2). Related to the order of new

Unlike before, once the new object position changes, the result cannot be output ... That's because if we add properties and methods in the form of a collection, be sure to write the new object behind him or the result will not output. The truth is actually very simple:

The Red Arrows are the first thing we add to the process of creating an object, and the blue arrows indicate that the second method is not available because we cannot return multiple sets of properties and methods between the definition function and his prototype.

(3) General-purpose problems

In contrast to adding properties to a Function object, the properties that are added by the prototype are generally better, and only the function object is added, and the other function objects cannot be used. Because this addition only adds properties to the inner space of the function, you must call the function to use it, and use the prototype as much as possible, considering versatility. (PS: Just suggest that you can come along with your temper.) )

(iv) Inheritance characteristics (emphasis)

The so-called inheritance, is to want the son has some of the characteristics of the father, that is, the son of fathers. Inheritance is very well implemented in other languages, because there are keyword extand, and this word specifies inheritance. However, in our JS scripting language, there is no such keyword, which makes it difficult to implement inheritance in the script features.

Object-oriented language (OOP); All three features are polymorphic, inherited, encapsulated. JavaScript is a kind of object-oriented language, and of course it has these features. So it is wrong to say that there are no inherited features in the script.

You may ask, the script does not have the keyword Extand, how to achieve inheritance? The script has its own inheritance mechanism, which is the prototype chain. The characteristics of inheritance can be realized through the prototype chain.

Before we say the prototype chain, let's look at this thing:

Add a little ... The function n can use the variable, a, and the inheritance is not related, because the variable has transitive characteristics within the global domain.

The next step is to focus on the inheritance mechanism of the prototype chain.

Look at an example:

By the right side of the relationship, B can be implemented using the content of a. Similarly, if you want a to invoke the contents of B, you can also use such a prototype chain. This is the most simple answer to ...

So I want to make calls to each other? His organization diagram should look like this: Construct the program:

Let's look at the results of the output;

The first output result is correct, and the inheritance of B to a is realized;

The second result output, does not implement a to B inheritance:

This problem shows that there is no problem with the structure, and the problem is in our program. Look at this program again: the real structure of the framework:

All right.. The basic knowledge of so many prototypes has been introduced, there is a place to optimize the prototype chain problem;

I did a lot of guessing when I was optimizing the prototype chain, but I couldn't use it, so I'm just going to talk about one thing that I can use here:

Prototype chain optimization:

The usual, look at the program:

Note that the prototype address pointers for both A and B point to a, but the content in the B prototype is freed, meaning that it inherits the attributes of a, but cannot preserve its own attributes. So, when we use it, we write the characteristics of the public into the prototype, and then we optimize it through this relationship, which preserves the commonality and retains its own characteristics, with the best of both worlds.

OK, well, the advanced features in JavaScript are basically the focus of these, these are the basis of our study of the key to the content, if the understanding is not good, the basic is to see the heavenly book, or that sentence, Bo Master literary Talent Limited, if there are errors, hope that timely correction ... If it is helpful to your study, it is my great honor, thank you.

JavaScript Advanced Features

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.