The This in JavaScript

Source: Internet
Author: User

In the case of JS, remember this: the object instance calls the function where this is, then this represents which object instance.
function test () {alert (this.x);} var o = {}; o.x = 1; O.M = test; O.M (); 1 Console.group ("xxxx"); function test () {this.x = 1;} var o = new Test (); alert (o.x);//1 Console.log (o.x); Console.groupend ("xxxx"); function Test3 () {  this.x = 3;} Test3 (); alert (x);//This function, this refers to the window, it can be understood, test3 () is window.test3 ();//So the This in Test3 () refers to the window, so x is a global variable. var o = {prop:37};function my () {  return this.prop;  alert (this.prop);} O.F = My;console.log (O.F ());//37, note the difference between O.F () and O.F. The specific differences between the two are not fully understood for the time being. Console.log (O.F);  My () function C () {  this.a = Notoginseng;  return {a:40};//My understanding is: the equivalent of re-assignment to a, the value is modified to 40. }o = new C (); Console.log (o.a); function C () {  this.a = PNS;  THIS.A = 90;} o = new C (); Console.log (O.A);

There is also a link to this: http://segmentfault.com/a/1190000000638443, which is well written, the specific content is as follows:

A question about this is answered in a previous time, and then the summary is recorded.
In JavaScript functions, each function can receive two additional parameters, in addition to the formal parameters defined at the time of declaration: this and arguments. Here's a look at the role of this and its different points under different scenarios. The value of this (that is, its point depends on the mode of invocation), in JavaScript it is clear that this point is roughly four cases:

1. When the function calls the pattern, this points to windowfunction AA () {  Console.log (this)}aa ()                 //window2. When the method invokes the pattern, this points to the object where the method is located var a={}; A.name = ' Hello '; a.getname = function () {  console.log (this.name)}a.getname ()         //' Hello ' 3. When the constructor mode is This points to the newly generated instance function Aaa (name) {  this.name= name;  This.getname=function () {    console.log (this.name)  }}var a = new Aaa (' Kitty '); A.getname ()        //  ' Kitty ' var B = new Aaa (' Bobo '); B.getname ()        //  ' bobo ' 4.apply/call call pattern, this points to the first parameter in the Apply/call method var list1 = {Name : ' Andy '}var list2 = {name: ' Peter '}function D () {  console.log (this.name)}d.call (list1)     //  ' Andy ' D.call (LIST2)     //  

  

The This in 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.