The point of the closure function this in JavaScript closures

Source: Internet
Author: User
Tags closure

Before reading the article, please read this article of Nanyi teacher first http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html

Written by the predecessors, and written very well, there is no need to repeat it. Just add some of Mr. Ruan's article did not say. by the way.

Quote a word: "The closure is functions that return function" (the source has been forgotten)

Types of closures: cyclic closures, function closures

Closure Features: External access function internal values, function internal variables are not recycled

The this point of the closure function in the function closure type.

Consider the following code:

var name = "Window";        var object = {            Name: "Object",            getnamefunc:function () {                return function () {                    return this.name;                };            }        };        var obj = {            Name: "Object",            GetNameFunc:object.getNameFunc ()        };        function foo () {            return this.name;        }        Console.log (foo ());//window         Console.log (Object.getnamefunc ());  Window         Console.log (Obj.getnamefunc ());//object

The closure function cannot directly access the This object that contains his function because the this point of the two is not the same.

The This of the external function points to the object that called him, and the inner function's this point is not difficult to understand because it points to the global object.

Analogy with the following two function calls

Foo ();//window
Object.getnamefunc () (); Window

In fact, when calling Object.getnamefunc (), as if the global object returned a function, the returned function is no different from the Foo function.

When this returned function is called in the global, the this of the function naturally points to the global object as if it were called the Foo function.

When Object.getnamefunc () is inside obj, the returned closure function is the property of obj, when this of the closure function points to the Obj object.

The point of the closure function this in JavaScript closures

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.