Supplement the "this" keyword in JScript

Source: Internet
Author: User
In the article "how to use this keyword in JavaScript", I have demonstrated the 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 = -1 00 ;

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:
 VaRFun= 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 );
}

The two tostring () methods, andThisWhat are their relationships? See the following example:

VaR Jsclass =   New Jsclass ();
Jsclass. tostring ();
Jsclass. tostring ();

The result is 100 and-100. The jsclass here is actually a javascript language mechanism, throughNewKeyword to create a new instance. What is jsclass? They are actually object instances. They only look like a function and a class description. For this problem, in the first jsclass. tostring () methodThisIt refers to the newly created instance, and in the jsclass. tostring () methodThisIs worth the jsclass object instance.

Understand the twoThis InstituteThe different nature of what is referred to can better understand why Javascript is called the object-based language and the essential difference between it and the object-oriented language.

Let's talk about the second stage later.ThisWhen the eval method is usedThisWhat is it? Let's see:

Alert ( This   === Eval ( " This " ));

What is the result? Yes true! This is becauseCodeThe executed scope 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.