Share here's awareness and a little solution to the face of JScript. JScript and JavaScript are almost almost (of course different), but the contents of this article can also be used in JavaScript
JScript supports some properties of the object, but his THIS pointer is very strange, when there is a member function in object OBJ1 to use this, if you have some object OBJ2 reference the function, then this THIS is not Obj1, but Obj2.
Let's take a look at one example:
this is the element "p" p>
Function Obj1 () {// class OBJ1
THIS.INNNERTEXT = "this is obj1";
}
Function Obj1.prototype.fun () {
Alert (this.innertext);
}
Var O1 = new obj1; // Object O1
O1.Fun (); // Show "this is obj
1
"
TEXT.ONCLICK = O1.Fun; // Binds the member function of the object O1 to the HTML element text, this is another click "this is the element" p ". You will find that this is the element" p "is displayed. , Not this is obj1.
script>
body> html>
From the above example we can know that the function references in JSCRPT only references the entry of the function, and does not save the object's reference. And this is just a simple point to the object of the call function. Jscript does not have an object pointer this kind of thing (maybe I don't know, if you know the article, please tell me (QQ: 123737)).
We will write like this when we usually write some ONCLICK events to handle the HTML element.
method one:
Function fun (obj) {
Alert (Obj.innertext);
}
script>
Element p>
Method Two:
Element P>
Function fun () {
Alert (this.innertext);
}
TEXT.ONCLICK = FUN;
script>
The following two methods are the same, maybe you will find the way the method is directly in direct display. Method 2 of FUN is more rational. But the method of the fun (this) is too much trouble, change the code to:
Function fun () {alert (this.innerText);
}
script>
Element p>
You will find this script that you can't work. why? ?
When you use the method, it is actually the case that the ONCLICK's handling event is like this:
Function anonymous () {fun (this)}
That is, IE creates an anonymous function to the OnClick event and calls the FUN function in the function. Since the object text is called, this is transmitted to the FUN function to the reference reference of the TEXT. At this time, the grown OBJ in FUN points to Text. If you write the event binding:
Element p>
Since the function Anonymous is called Fun (), it is not pointing anywhere if you use this this in Fun. If you alter (this), you will find his value is undefined.
In Method II, OnClick handling event is FUN, so this is available, it points to TEXT. But don't write the definition of fun in Method II:
Function fun (obj) {
Alert (Obj.innertext);
}
When the TEXT responses to the OnClick event call OnClick, it is not passed to FUN, then the OBJ is undefined.
The problem is clear, but what should I do when we want to respond to HTML events, what should I do when I exist in the object? (A basic processing method is based on pure event driven)
We can do this:
Click this p>
Function Obj () {
THISINNNERTEXT = "this is obj";
}
Function Obj.prototype.Fun () {
VAR Self = this.obj; // Get reference to OBJ1. I have learned that Delphi knows self.
// jscript This is not re-assigned, so use Self. People who have learned Delphi know what Self is.
Alert (Self.innertext);
}
Var Obj1 = new obj;
TEXT.OBJ = Obj1; // Add a new attribute OBJ to Text and is an OBJ1 reference.
TEXT.ONCLICK = Obj1.fun;
script>
Click the Click this results to display "this is obj".
Using this method can use JScript pure event-driven programs.