"This" keyword in JScript how to use the _javascript technique of supplementary materials

Source: Internet
Author: User
In the article "How to use this keyword in JavaScript", I have cited the 8 kinds of this keyword in JavaScript and JScript. This does not find that there are also two kinds of this keyword used at the time did not say, now added. And by using the first this keyword, we can better understand the nature of JavaScript as a object-based language.

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


function Jsclass ()
{
}

JSClass.prototype.m_Properties = 100;

JSClass.prototype.ToString = function ()
{
alert (this.m_properties);
}

The this.m_properties in the method ToString is 100, what about the following definition?

function Jsclass ()
{
}

jsclass.m_properties =-100;

Jsclass.tostring ()
{
alert (this.m_properties);
}

What is this this.m_properties in the ToString? IS-100. Are you sure? This is not necessarily, it depends on how we call this tostring method.

Jsclass.tostring ();
var fun = jsclass.tostring ();
Fun ();
The results of this operation are:-100 and undefined. Really depressed ha, how to run fun get-100? This is required to assign a value to the fun:

var fun = function () {jsclass.tostring ();}
Fun ();
Oh, this is-100. Seems to be a bit of nonsense, ultimately not or call jsclass.tostring ()? We'll talk about this later and see what it's like to put these two jsclass together.


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 these two ToString () methods and the This keyword inside? Look at the following example:

var jsclass = new Jsclass ();
Jsclass. ToString ();
Jsclass.tostring ();
The results are: 100 and-100. The Jsclass is actually a language mechanism for JavaScript, a new instance created by the description of the New keyword. And what is Jsclass? They are examples of objects, but they look like functions, as well as descriptions of a class. For this question, the first jsclass. This in the ToString () method refers to the newly created instance, and the This in the Jsclass.tostring () method is worth Jsclass this object instance.

By understanding the different nature of the things that are referred to in these two this, you can better understand why JavaScript is called object-based language, and more clearly the essential difference between it and object-oriented language.

I'll see you later. The use of this in the second is what is this when you use the Eval method? Look:

Alert (This = = eval ("this"));
What is the result of     ? It's true!. This is because the scope that the eval code is executed is the current page itself.

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.