How to use the "this" keyword in JScript to supplement the material _ javascript skills

Source: Internet
Author: User
In JScript, the & quot; this & quot; keyword usage supplementary material is described in the article "describes how to use this keyword in JavaScript, I have cited eight methods of this keyword in JavaScript and JScript. This does not mean that there are two ways to use the this keyword that I did not mention at the time. I would like to add a description. The description of the first this keyword allows us to better understand the nature of JavaScript as an Object-Based language.

One is related to the definition in the JavaScript class. we know that when we define the following class:


Function JSClass ()
{
}

JSClass. prototype. m_Properties = 100;

JSClass. prototype. ToString = function ()
{
Alert (this. m_Properties );
}

This. m_Properties in the ToString method is 100. what about the following definition?

Function JSClass ()
{
}

JSClass. m_Properties =-100;

JSClass. ToString ()
{
Alert (this. m_Properties );
}

What is this. m_Properties in ToString? Yes-100. Yes? This is not necessarily the case. it depends on how we call the ToString method.

JSClass. ToString ();
Var fun = JSClass. ToString ();
Fun ();
The running result is-100 and undefined. How can I get-100 for fun? You need to assign values to fun in this way:

Var fun = function () {JSClass. ToString ();}
Fun ();
Well, this is-100. Isn't JSClass. ToString () called in the end? Let's talk about it later. let's take a look at the situation where the two JSClass are combined?


Function JSClass ()
{
}

JSClass. m_Properties =-100;
JSClass. prototype. m_Properties = 100;

JSClass. ToString = function ()
{
Alert (this. m_Properties );
}

JSClass. prototype. ToString = function ()
{
Alert (this. m_Properties );
}

What is the relationship between the two ToString () methods and the preceding this keyword? See the following example:

Var jsclass = new JSClass ();
Jsclass. ToString ();
JSClass. ToString ();
The result is 100 and-100. Here, jsclass is actually a JavaScript language mechanism. a new instance is created through the description of the new keyword. What is JSClass? They are actually object instances. they only look like a function and a class description. For this problem, this in the first jsclass. ToString () method refers to the newly created instance, and this in the JSClass. ToString () method is worth the JSClass object instance.

By understanding the different nature of the two things referred to by this, we can better understand why JavaScript is called the Object-Based language, the essential difference between the language and Object-Oriented is clearer.

Let's talk about the second method of using this, that is, when using the eval method, what is this at this time? Let's see:

Alert (this = eval ("this "));
What is the result? Yes true! This is because the scope of eval code execution is what the current page itself says.

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.