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:
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?
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.
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.