Introduction to closures in JavaScript _javascript tips

Source: Internet
Author: User
Tags closure

The so-called closure should mean that the internal function reads a variable other than the current function, that is, the context in which it was created.

Copy Code code as follows:

function Hello () {
var char = "Hello,world";
function print () {
Console.log (char);
};
return print ();
}

Note that the print function here refers to the char variable of the external Hello function, so here we can return a
Copy Code code as follows:

Hello,world

This function, in a sense, should be attributed to the scope. Of course, we have no way to directly access char unless we make a mistake when declaring the variable. Such as
Copy Code code as follows:

function Hello () {
char = "Hello,world";
function print () {
Console.log (char);
};
return print ();
}

Just because of the lack of a var.

Copy Code code as follows:

Here, hello turns into a closed bag. Closures are a special kind of object. It consists of two parts: a function, and the environment in which the function is created. The environment consists of any local variables that are in the scope when the closure is created.

Javscript closure and this

Note that it may be problematic to read this and arguments.

Copy Code code as follows:

function Hello () {
This.char = "Hello,world";
function output () {
char = "I ' m no Hello World";
Console.log (This.char);
};
return output ();
}

Of course, this example is not appropriate, so, we need an additional example to explain this problem, the following reference to a "JavaScript advanced programming," an example to illustrate this problem.
Copy Code code as follows:

var name = "the window";

var object = {
Name: "My Object",

Getnamefunc:function () {
return function () {
return this.name;
}
}
};
Object.getnamefunc () ()


It's just that usage, and the solution is to save a temporary variable that, as in the previous article on some knowledge of this one about JavaScript.

Copy Code code as follows:

var name = "the window";
var object = {
Name: "My Object",

Getnamefunc:function () {
var that = this;
return function () {
return that.name;
}
}
};
Object.getnamefunc () ()


javscript Closures and read-write variables
It is noteworthy that we can modify these variables if we do not handle our variables well.
Copy Code code as follows:

function Hello () {
var char = "Hello,world";
return{
Set:function (String) {
return char = string;
},
Print:function () {
Console.log (char)
}
}
}
var say = hello ();
Say.set (' New Hello,world ')
Say.print ()//New Hello World

Javascript Closures and performance

References to MDC

Copy Code code as follows:

It would be unwise to create functions in other functions without the need for some special tasks, as closures have a negative impact on script performance, including processing speed and memory consumption.

The article also said.
Copy Code code as follows:

For example, when you create a new object or class, the method should usually be associated with the object's prototype, rather than the object's constructor. The reason is that this will cause each constructor to be invoked and the method will be assignable once (that is, for each object creation).

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.