Implement VB.NET, C # [Entrust Delegate] with JScript
By: [lostinet {lostinet@21cn.com; lostinet@chongjian.com}] [Sun Mar 17 UTC 0800 2002]
VB.NET, C # entrusting objects and methods into an object. Today based on JScript grammatical characteristics, similar functions are made.
Insufficient: If Function is not member of Object, Object must allow new properties to be filled. For example, JScript objects, DHTML DOM's Window, Document, etc. If you don't have new ActiveXObject ("Microsoft.xmLDom") If Function is not Object member, you must fill a var theIndex = "DELEGATE FUNCTION " new date () " math.random (); => Object [theIndex] = function, theIndex will also appear in the structure of the For (VAR i IN Object).
Note: Once Delegate is referenced, the relevant object, Function will not be released. To completely release Object, Function, you must first release the relevant Delegate
The script and explanation are as follows:
/ / Returns the call of FunctionSelf, any function calls FunctionSelf will return the function itself in the function functionself () {returnctionSelf.caller} / * Return to encapsulate Obj, func Delegate, such as known objects and functions: obj1.func1, then call Obj1 .func1 (1, 2, 3) and call delegate (Obj1, Obj1.func1) (1, 2, 3) will be similar (except for the shortcomings mentioned above)
In addition to this function is disclosed, other functions should not be called. OBJ: Specify object funcorindex: Specifies a reference to the function, or a name belonging to OBJ * / function delegate (Obj, funcorindex) {var delegate = new function ("" FunctionSelf (). call (functionself (). arguments "); Delegate.Object = Obj; delegate.call = delegate.call; if (funcorindex) ==" string ") {delegate.index = funcorindex; delegate.Function = Obj [funcorindex];} else {delegate. Index = NULL; DELEGATE.Function = funcorindex;}}} = "function") throw ("No Specified Function!"); Return Delegate;} // When Delegate is called, this function is Will be called. Function delegate.call (arguments) {/ * involves the number of function.arguments allows dynamics, so I select EVAL to execute. When a delegate is called, a global variable represents a collection of Object, Function, and Arguments currently encapsulated in order to be called * / //. Delegate.stack.push (this, arguments) (Var (var i = 0; i // When Delegate is nessed, // is not processed in the middle. Then return to the outermost IF (! This.index) delete this.Object [theIndex]; DELEGATE.STACK.POP (this); thiw (x);} if (! This.index) delete this.object [ TheIndex]; // If it is a custom index, then delete delegate.stack.pop ();} // Newly built a global variable Function Delegate.stackObject (delegate, arguments) {this.delegate = delegate; this .Aguments = arguments;} // Return the current object function delegate.stack () {return delegate.stack.object;} // stack data function delegate.stack.Array () {} // Because the call is a stack form So the data is also stack. // Press the status of the current delegate called. Function delegate.stack.push (delegate, arguments) {if (delegate.stack.position) == "undefined") DELEGATE.STACK.POSITION = -1; delegate.stack.position ; delegate.Stack.Object = New Delegate.StackObject (delegate, arguments); Delegate.Stack.Array [Delegate.Stack.Position] = Delegate.Stack.Object;} // Releasefunction Delegate.Stack.Pop () {Delegate.Stack.Array [Delegate.Stack. Position] = NULL; DELEGATE.STACK.POSITION -;} // below, is an example: Function myalert (str) {try {wscript.echo (str)} catch (x) {} try {al (str)} catch (x) {}} Var obj = new object (); obj.value = "the obj.value :)"; Function Func (A, B, C) {var str = ""; var count = 0; for (var i in this) { Count ; STR = I ":" TypeOf (this [i]) "/ n";} var str = "A, B, C =" A ":" B ":" C "/ nthis. Value = " this.Value " / n " count " / n " str; mylert (str);} var delegate = delegate (OBJ, FUNC); // Transfer function reference DeleGate (3, 33, 333); OBJ. Callthefunc = func; // or: obj ["CallThefunc"] = func; delegate = delegate (Obj, "CallThefunc"); // Pass the name Delegate (4, 5, 6); var xml = new ActiveXObject ( "Microsoft.XMLDOM"); var xmlo = new Object (); xmlo.xml = xml; xmlo.name = "xmlo"; xml.onreadystatechange = Delegate (xmlo, onreadystatechange); xml.load ( "TEST1.XML"); var xml2 = new activ ("microsoft.xmldom"); var xmlo2 = new object (); xmlo2.xml = xml2; xmlo2.name = "xmlo2"; xml2.onreadystateChange = Delegate (XMLO2, OnReadyStateChange); XML2.Load ("Test2.xml"); Function OnReadyStateChange () {if (this.xml.readyState! = 4) Return; // not ready if (this.xml.Parseerror.Elet (this.name this.xml.parseerror.reason); Else Mylert THIS.XML.XML); --------------- 2002.3.8: ------------- Function functionsel () {Return FunctionSelf.caller;} function delegate (Obj, funcorname) {var delegate = new function ("", "Return FunctionSelf (). Invoke (FunctionSelf (). arguments") Delegate.Arguments = new array (); delegate.Object = Obj; delegate.unique = "delegateunique" math.floor ((new date (). gettime () math.random ()) * 1000); if (Typeof (funcorname) == "string") {delegate.funcname = funcorname; delegate.function = obj [delegate.function.FuncName = null; delegate.function = funcorname;} delegate.Invoke = Delegate.Invoke; delegate.Detach = Delegate.Detach; delegate.SetArguments = Delegate.SetArguments; delegate.PreInvoke = Delegate.PreInvoke; delegate.valueOf = new Function ( "", "return /" " delegate. Uniqueename "/" "); delegate.tostring = new function (" "," returnen /"" dlegate.uniquename "/ ""); Return delegate;} function delegate.getcaller () {var CID = functionSelf (). Caller.caller.callid; return delegate.coll [CID];} function delegate.getdeLegate () {var Cid = functionSelf (). Caller.caller .CallID; return Delegate.Coll [cid] .Delegate;} function Delegate.PreInvoke () {this.SetArguments (Delegate.PreInvoke.arguments); return this;} function Delegate.SetArguments (args) {if (args == null ) args = new array (); this.arguments = new array (); for (var i = 0; i Var funcname = this.funcname || CID; if (this.funcname == null) this.object [funcname] = this.function; var res; var exception; try {res = ev - "new function ('', / "FunctionSelf (). CallID = '" CID "; RETURN DELEGATE.COLL ['" CID "]. Delegate.object ['" funcname ""] (" strarguments ") / ") ()" );} Catch (x) {exception = x} if (this.object && (this.funcname == null) delete this.bobject [funcname]; DELEGATE.COLL.Remove (CID); if (Exception) THROW Return res;} function delegate.detach () {this.object = null; this.function = null; this.funcname = null; this.uniquename = null;} function delegate.evalcaller (DELEGATE, ARGS, CID) { THISDELEGATE = Delegate; this.arguments = args; this.callid = CID;} function delegate.coll ()} function delegate.coll.insert (delegate, args) {i (typeof (delegate.coll.length) = = "undefined") DELEGATE.COLL.LENGTH = 0; DELEGATE.COLL.LENGTH ; var CID = delegate.uniquename "call" math.floor ((new date (). gettime () math.random ()) * 1000); var evAlcaller = new delegate.coll (CID); delegate.coll [CID] = evAlcaller; R ETURN CID;} function delegate.coll.remove (CID) {delete delegate.coll [CID]; DELEGATE.COLL.LENGTH -;} ------------------- -example: function myjoin () {var str = ""; for (var i = 0; i ---------------