This project has moved and is read-only. For the latest updates, please go here.

Access to "this"

Jan 3, 2012 at 3:12 AM

First: I have to say kudos!  For about a year I have been a serial JavaScript Engine shopper.  I've tried just about everything out there and have reached various degrees of disappointment with each until this one.  I think you've hit the nail on the head.  (Feature complete: check.  Performance: check.  Integration with .NET: check.  Thread safe (at least for what I am doing): check. License that I can live-with: check.)  Outstanding!

OK.  With that out of the way: I think I am missing one thing.  I want to create a CLR function that is a method in an existing JavaScript class.  (I want to add a couple methods to the String class.)  I'm up-to-speed with adding functions and can register a CLR function into a JavaScript object with no problem and can probably easily locate the String function's prototype object.  The one thing I think I'm missing is: in my CLR function, how do I access the JavaScript's "this"?

In other words: I will create a function called encodeHtml() that returns a string and I will get that added as a function into the right prototype object but how does encodeHtml() access the "this" reference to the string that is being operated upon?

Jan 3, 2012 at 7:38 PM

This should be fairly simple. To get a reference to the this-object in javascript you can do one of two things (that I know of). What I would do is to subclass ClrFunction for each method you want to add to the prototype-chain of a already existing project. On the ClrFunction you can override the CallLateBound (if memory serves me right), it gets the this-reference.


The other thing you can do is create static methods on a class, and flag them that they require the this-object. Look at the source-code for StringInstance for this. Though I would recommend the previous solution.