Interpreting this in JavaScript

Source: Internet
Author: User

When using this, 1. When executing a function, determine whether the function is an object method or a separate function? Separate function this = window; object method, this = object. Copy the code function UseThis () {console. log (this = window); this. instancePro = 1;} UseThis. objPro = 2; UseThis. objMethod = function () {console. log (this. objPro);} UseThis (); // true no matter how deep the nesting is, this = windowconsole in the function when executing the function. log (instancePro); // 1 var useThis = new UseThis (); // false the current value is A constructor. this In the constructor is the console of the new instance. log (useThis. instancePro); // 1 UseThis. objMethod (); // 2 the current function is an object method. this = UseThisvar fn = UseThis. o BjMethod; fn (); // undefined copy the code to open the test page and start the debugger. 2. After the function is returned by the bind method, this points to the first parameter of bind. 3. Execute the function through call (apply). this points to the first parameter of call (apply. Copy the code/* call the function twice */function doubleBind () {console. log (this. doubleVariable);} (function () {console. log (this. doubleCalendar); // 2 doubleBind. call ({doubleVariable: 1}); // 1 }). call ({doubleVariable: 2}); copy code 4. a function that calls bind first, and then uses call for execution. this points to the first parameter of bind. /* Bind the function to Bind the return function and call */function funBind () {console. log (this. pro);} var relFun = funBind. bind ({pro: 2}); relFun. call ({pro: 3}); // copy the code var con_inObj = {variable: "sprying", cons_fun: function () {console. log (this. variable) ;}} var new_obj = new con_inObj.cons_fun ();//? <! -- From front-end stew --> var x = 5; var example = {x: 100, a: function () {var x = 200; console. log ('a context: % s, var x = % s', this. x, x) ;}, B: function () {var x = 300; return function () {var x = 400; console. log ('B context: % s, var x = % s', this. x, x) ;}}, c: function () {var other = {x: 500}; var execB = this. B (). bind (other); execB (); return execB ;}} console. log ('example. x: '+ example. x); example. a (); example. B (); example. a. call ({x: 9999}); var execB = example. c (); execB. call ({x: 9999}); copy the code

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.