This of JavaScript

Source: Internet
Author: User

Global scope of this

 this = = window   //truethis. A  = 8  window.a//  8

This of the general function

function thistest () {   returnthis = = = Window   //true  function  thistest () {   ' use strict '   returnthis = = = Undefined   //true

The this in a function as an object method

 var o  = {  x:1,  y:2,  f:function() {        returnthis. X}}O.F ()  //x    as the object method function in this, This object is pointing to

On this on the object prototype chain

var o  = {f:functionthis. A+ this. B}}var obj= = 90  = ten; OBJ.F ()  //The This   on the prototype chain, pointing to obj

This in the constructor

function fuc () {   this. A = PNs;}  var New fuc (); F.A    // Panax   Notoginseng

function func () { this. A =n; return 1;} var New func (); FUNC.A // 65

function   funct () {   this. A =n;     return {x:1};} var New funct (); Funct.x   // 1

When you create an object from the new constructor
1 There is no return or return at the time of the basic type. returns this. (This prototype points to the function where this is located)
2 Returns the object type, returning the object as the return value

Call/apply method and this

 function   Add (c) { return  this . A+this . B+c}  var  o = { A:2,b:4}add.call (o,  4) // 10 call the first parameter passed in the object to be used as this, followed by the arguments in the function argument list, separated by commas 

function Add1 (c) {  returnthis. A+ this. B +C}var o1 = {A:2,b:4} Add1.apply (o,[4])  //    Apply the first parameter passed in the object to be used as this, followed by the arguments passed in the function argument list, passed in as an array

function fuc (x, y) {
Console.info (X,y,this)
}

Fuc.call (1,7,6); 7 6 Number

Fuc.call (NULL); Undefined undefined Window

Fuc.apply (NULL); Undefined undefined Window

Fuc.call (undefined); Undefined undefined Window

Fuc.apply (undefined); Undefined undefined window in general mode, call,apply incoming null,undefined point to the window

Bind method with this

function Z () {   returnthis. a}; var g = Z.bind ({a: "*"}); g ()   //"the" """var o = {a:4, f:z,g:g };O.F ()  ///4   function As Object method, this points to O object, all O.F () is 4//  "89 The  bind method binds Z () to the point of this in {a: "_"}

This of JavaScript

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.