[Basic]javascript----This

Source: Internet
Author: User

Excerpt from the advanced programming of JavaScript

Ps: Very good book, you can go directly to read, here is just record. Recommend a computer ebook website: the Leather Bookstore

Search information: Mdn,w3cschool,google

    • the execution environment of an anonymous function is global , so this is pointing to the window
    • function is called, and its active objects are automatically given two special variables: This and arguments
    • Call (), apply (), bind () point to its specified object

This: How the function is called determines the value of this

----in global context

Point to window, point to undefined in "use strict" mode

---------------------------

In a word, this is determined by the run-time context , who is the runtime context, and this points to who

function A () {    returnthis  }

A function is in the global environment

A ()       //equivalent to the following WINDOW.A ()

So this is pointing to window

------------

var name= "the Window"; var object ={    "My object",    function() {        return function () {            returnthis. Name;  anonymous function, Closure          };}    ; Alert (Object.getnamefunc () ()); The Window

Object.getnamefunc () returns an anonymous function because the execution environment of an anonymous function is global , so this point points to the window

-------------------

var name= "the Window"; var object ={    "My object",    function() {
var that =this returnfunction() { return to that . Name; anonymous function, Closure }; // The Object

var = This is the object in the environment, so this is a pointer to object, which is obvious, so that.name is also pointing to Object.name

----------------

var o = {prop:37}; function Independent () {  returnthis = Independent;     // Logs 37

Equivalent to
var o = {prop:37};
O.F = function () {
Return This.prop
}
Console.log (O.F ()); Logs 37
So obviously this is in O environment, so this is pointing to O


--------------------------

var name= "the Window"; Var object ={"name": "My Object"};console.log (    (object.getname=function() {    the anonymous function assigns a value and executes, so point to window        returnthis. Name;    }) ());    //  Console.log (Object.getname ());  // My Object

There is a C-pointer feeling, the brain is a direction of the arrow, want the other end of the arrow, you know this point to WHO

---------------------------------------------------------------------------------------------------------------

1 var name = "the Window"; 2 3 var object = {4     Name: "My Object", 5     getname:function() {6         return This}9 (object.getname = Object.getname) (); The Window    
    1. The 9th line will appear in Chrome

Uncaught Typeerror:cannot Read Property ' GetName ' of undefined

and IE11 under no problem, why????

[Basic]javascript----This

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.