JavaScript-Learn This

Source: Internet
Author: User

This

  In a generic strongly typed language, this is the object itself, which can be used in JavaScript

    The value of this is part of the execution context environment

In fact this is not very difficult to immediately, just remember two points on it

    That's who caller (the caller is who this is pointing to), and if there is an assignment statement, change the type

Let's see a couple of examples.

function Fd () {    this. name= "Test";    Console.log (this);}; var f1=New Fd (); // Fd {name: "Test"} Fd (); // window

The first time here is through new, so the caller is F1, and the second is executed directly, and FD is just an expression, which is equivalent to a global function, so he is called by the window

var obj={    x:ten,    fn:function  () {        Console.log (this );        Console.log (this. x);    }};o Bj.fn (); // obj,10 var d=obj.fn;d (); // window ""obj.fn.call (); // equivalent to the above code

What about the code above?

In fact, because Obj.fn is assigned to D, and then we call D (), the steps are:

> Our current global context

> Executes d (), enters the FN context, and all callers are window

So here's a proof of the argument that there are assignment statements that change the type.

And of course, there 's a special case.

  

Because F () is a normal function, all, this points to the window

JavaScript-Learn This

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.