Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Statement · Dec 11, 2010 at 12:16 AM · javascript-specificextension-methods

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Statement · Dec 17, 2010 at 03:58 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Dec 17, 2010 at 04:39 PM 0
Share

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

avatar image Statement · Dec 17, 2010 at 05:02 PM 0
Share

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

avatar image Mike 3 · Dec 17, 2010 at 05:30 PM 0
Share

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

avatar image
0

Answer by amirabiri · Oct 22, 2011 at 05:06 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Input.GetButtonDown not working at ALL 1 Answer

Reload Level on Collision 4 Answers

C# to Unityscript problem. 1 Answer

How to declare and use a enum variable in Javascript? 2 Answers

Unable to load script, when referencing an extension method 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges