Jquery Study Notes: What does this mean?

Source: Internet
Author: User

This sentence:
If (window = This) return New jquery (a, c );
I will perform some tests on this object:
1. It represents the window object.
Test code:
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> test </title>
</Head>

<Body>
<Script language = "JavaScript" type = "text/JavaScript">
Window. Name = "window_name ";
Alert (this. Name );
This. Alert ("OK ");
</SCRIPT>
</Body>
</Html>
The window object refers to the entire ie window, including the menu bar and status bar. For example:
Window. Status = "hello ";
Window. moveTo (-12,-12 );

2. It represents the current class
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> test </title>
</Head>

<Body>
<Script language = "JavaScript" type = "text/JavaScript">
Class1 = function ()
{
VaR name1 = "OK ";
This. Name = "ASD ";
Alert (name1 );
Alert (this. Name );
}

VaR o = new class1;
Alert (O. name1 );
Alert (O. Name );
</SCRIPT>
</Body>
</Html>
When tracing with vs, in class class1, this is an empty object. After it gets the name, it can be accessed outside the class. Therefore, this type can be used as a public attribute of the class. Private attributes are defined by VAR and cannot be accessed from outside.

You can also use this to distinguish between public and private methods.
Class1 = function ()
{
Function AA ()
{
Alert ("AA ");
}

This. BB = function ()
{
Alert ("BB ");
}
}

VaR o = new class1;
Alert (O. aa );
O. AA ();
Alert (O. bb );
O. BB ();

If you are outside the class, you can use class1.prototype to replace this
Class1.prototype. Cc = function ()
{
Alert ("cc ");
}

VaR o = new class1;
Alert (O. CC );
O. CC ();

3. What does this mean when this occurs in a function call?
This often appears in functions, such
<A herf = "#" onclick = "dosomething (this);"> III </a>
Function dosomething (ABC)
{
ABC. innertext = "OK ";
}
During running, this indicates Element.

4. What does this represent when it appears in a function?
<A herf = "#" onclick = "dosomething ();"> III </a>
Function dosomething ()
{
This. innertext = "OK ";
}
In this case, it is a window object, because dosomething belongs to window.
If you change
<A id = "A1" href = "#"> III </a>
A1.onclick = function dosomething ()
{
This. innertext = "OK ";
}
In this case, this is A1.

Originally, I encountered these situations, but later, in csdn again saw a piece of code (http://topic.csdn.net/t/20060517/22/4759575.html#), and caused a burst of tossing:
<HTML>
<Head>
<Script language = "jscript">
Function myclass ()
{
This. Name = "myname ";
This. showname = showname;
Function showname ()
{
Alert (getname ());
}

Function getname ()
{
Return this. Name;
}
}

VaR OBJ = new myclass;
OBJ. showname ();
</SCRIPT>
</Head>

<Body>
</Body>
</Html>
The system prompts a blank message at the end, instead of the desired myname. After tracking, I found that this indicates window, which indicates that my previous ideas are incorrect. There are several processing methods, such:
1: Upgrade getname to a shared method.
Function getname ()
Change
This. getname = function ()

2: Use call to specify
Alert (getname ());
Change
Alert (getname. Call (this ));

3: replace this with a variable.
Add in the first sentence
VaR A = this;
Then
Return this. Name;
Change
Return A. Name;

But I cannot find the theoretical support. Later, I found a section in "javascript object-oriented programming:
1.2 This and execution Context
......
Let's take a look at the execution context. What is the execution context?

If a method is being executed, the execution context is the object attached to the method.

If you are executing the process of creating an object (that is, creating an object through new), The created object is executed.

Context.

If a method is not explicitly attached to an object during execution, its execution context is a global object (

But it is not necessarily attached to global objects. Global objects are determined by the current environment. In the browser Environment

Global objects are window objects.

Defines global variables and global functions outside all functions attached to global objects, and defines local changes within the function.

Volume and local functions are not attached to any object.
......
Run the following code for testing:
VaR x = "global variables ";

Function Method ()
{
Alert ("X:" + X in the global method );
Alert ("This. X:" + this. X );
}

Function class1 ()
{
VaR x = "private variable ";

Function Method1 ()
{
Alert ("Private method x:" + x );
Alert ("Private method in this. X:" + this. X );
}
VaR method2 = method;

This. x = "public variables ";

This. Method1 = function ()
{
Alert ("public method x:" + x );
Alert ("This. X:" + this. X );
}

This. method2 = method;

// Constructor
{
This. Method1 (); // result: private and public variables in the public Method
This. method2 (); // result: global variables and public variables in the Global Method
Method1 (); // result: private variables and global variables in the private Method
Method2 (); // result: global variables and global variables in the Global Method
Method1.call (this); // result: private and public variables in the private Method
Method2.call (this); // result: global variables and public variables in the Global Method
}
}

VaR o = new class1 ();
Method (); // result: Global and global variables in the Global Method
O. Method1 (); // result: private and public variables in the public Method
O. method2 (); // result: global variables and public variables in the Global Method
In private methods, this points to global. This makes it awkward for us to write a program, so we use a code to forcibly convert it. This code is
If (window = This) return New jquery (a, c );
That is, if this is a window at this time, an object is created, the parameter is passed to the new object, and executed in the new object area. In this way, this indicates jquery.

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.