Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Extrakun · Apr 19, 2010 at 09:40 AM · javascript-specificoop

Extending from interface in JS?

What is the syntax for creating an interface, an abstract base class, and extending a class from it (In JavaScript)?

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

4 Replies

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

Answer by podperson · Oct 31, 2011 at 11:23 AM

Unity JavaScript now (as of 3.4, possibly earlier) supports implements, so the old best answer no longer holds.

Here, as an example, is an implementation of the QCAR ITrackableEventHandler interface in JavaScript.

 class TrackableEventHander implements ITrackableEventHandler {
     var tracked : boolean;
     
     function OnTrackableStateChanged(
         previousStatus : TrackableBehaviour.Status,
         newStatus : TrackableBehaviour.Status
     ){
         if( newStatus == TrackableBehaviour.Status.TRACKED ){
             tracked = true;
         } else {
             tracked = false;
         }
     }
 }
Comment
Add comment · Show 2 · 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 duck ♦♦ · May 16, 2012 at 08:13 AM 0
Share

does this mean you can use extends and implements in the same class definition? and can you add multiple "implements"?

avatar image guavaman · Aug 26, 2012 at 01:48 AM 1
Share

This page shows how to actually create an interface using the interface keyword. http://docs.unity3d.com/Documentation/$$anonymous$$anual/$$anonymous$$onoUpgradeDetails.html

And, yes, you can use both extends and implements.

avatar image
2

Answer by duck · Apr 19, 2010 at 11:52 AM

While Unity's Javascript does support inheritance and interfaces, it has the very limiting caveat that you can either inherit your class from an existing class, or declare one interface. Eg:

// in C# you can declare a combination of inheritance and multiple interfaces: class Apple : MonoBehaviour, ICollectable, IEatable, IThrowable { ... }

// Javascript - only one allowed, eg: class Apple extends BaseObject { ... }

// or: class Apple extends IThrowable { ... }

And as far as I can see, there's no way to actually define an interface in JS - you just have to define the interface in C#, and have it in an earlier compilation pass so that your JS script can see the type.

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
avatar image
1

Answer by Ashkan_gc · Apr 19, 2010 at 09:48 AM

you can not define interfaces in js but you can inherit from them using extend keyword i think you can create abstract classes using abstract keyword but i am not sure because this implementation of unity's js is unity's own implementation and there is not any specification available. i don't know what version of the ECMA script they support. you can just create abstract or virtual methods in you classes in C# or create interfaces n C# and use them in js. if you want to create an OO structure for your program use C# programming language.

Comment
Add comment · Show 1 · 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 guavaman · Aug 26, 2012 at 01:50 AM 1
Share

You can define them as of Unity 3.5. http://docs.unity3d.com/Documentation/$$anonymous$$anual/$$anonymous$$onoUpgradeDetails.html

avatar image
1

Answer by dentedpixel · Aug 01, 2013 at 09:31 PM

You can define an interface in Javascript, here is the syntax:

 interface MyInterface{
     function someMethod( param:int );
 }

then implementing the interface

 public class FooSubClass extends MonoBehaviour implements MyInterface{
 
 function someMethod( param:int ){
 
 }
 
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

Inherit from a behaviour (JS) 2 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

Picking a language... 1 Answer

Resetting a combo attack chain if next attack did not occur in time 1 Answer

Field of view, using raycasting 5 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