Before updating to Windows 10 you may need to return your Pro license. More information here

How can I write and use extension methods in JS?

In C# there are extension methods that can be written such as:

// Create an extension for string public static class MyExtension { public static bool IsCaptialized(this string value) { return Char.IsUpper(value[0]); } }

// And then use it void TestExtension() { string shouldBeCapitalized = "Capitalized"; Debug.Log(shouldBeCapitalized.IsCaptialized()); }

  • Can I write extension methods in JS?
  • Can I use extension methods written in C# with JS?

Progress update.

As I have been trying some C#/JS since I asked the question, it appears that JS does not recognize the extensions I wrote in C#. I placed the extensions in Standard Assets folder so that it would compile before the JS. That leaves me to think there isn't any way in JS to use extension methods and as such there is no way to write extension methods in JS, at least not for the present.

more ▼

asked Dec 11, 2010 at 12:16 AM

avatar image

28.1k 81 183 307

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Since no other answers have been posted and I haven't been able to find any solution through my own fiddling, I draw the conclusion that it's a simple case;

Javascript for Unity3D does not support extension methods.

more ▼

answered Dec 17, 2010 at 03:58 PM

avatar image

28.1k 81 183 307

Same conclusion I came to when I answered the same question two weeks ago

Dec 17, 2010 at 04:39 PM Mike 3

I'm sorry, I didn't notice any other answers here? Or is this a duplicate question?

Dec 17, 2010 at 05:02 PM Statement

Not a dupe, but similar. http://answers.unity3d.com/questions/26059/can-i-extend-vector3-using-javascript/26061#26061 . And eep, that wasn't two weeks ago

Dec 17, 2010 at 05:30 PM Mike 3
(comments are locked)
10|3000 characters needed characters left

There are no extension methods in JS, or in Unity's version of JS (which isn't traditional JS).

JS won't recognize C# extension methods because C# extension methods are not real artefacts added to the class, they are syntactic sugar. In essence they are just static methods. The C# compiler, as a feature of the language, knows how to "use" them from an object context given the right syntax. But as said - it's just a bit of magic, they are not real. Hence they are not visible as more than static methods from a cross-language point of view.

Generally speaking, if you want a "quick and easy" language to program in, use JS. If you come from a "heavier" computing background and would like to have the full power of a strong mainstream language at your disposal, use C#. For larger projects I'd recommend C#.

Either way don't expect to get the same from both languages, they are different.

more ▼

answered Oct 22, 2011 at 05:06 PM

avatar image

1.2k 78 62 103

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 11, 2010 at 12:16 AM

Seen: 2288 times

Last Updated: Oct 22, 2011 at 05:06 PM