How to use Interfaces in UnityScript/JavaScript? Is it even possible?

So I have tried using the keyword interface in JavaScript/UnityScript but that does not compile correctly when I try to implement the interface.

I have seen it used in Unity C#.

I want to know if you can achieve this with UnityScript/JavaScript or not. Is it even possible? Is there an equivalent way of achieve a similar goal to what Interfaces achieve?

According to the documentation you can use interfaces in UnityScript since Unity3.

    interface IFoo {
		function bar();
	}

	class Foo implements IFoo {
		function bar() {
			Console.WriteLine("Foo.bar");
		}
	}

EDIT: In order to get interfaces compiling and running correctly, you need to:

  1. The file name of the script defining the interface must match the interface name. e.g MyInterface.js contains the MyInterface interface declaration.

  2. The file using the interface needs to extend from MonoBehaviour (if the file is going to be attached to a game object)

  3. The file name using the interface needs to match the class name.

    // The following class is using the MyInterface interface.
    // It is contained in a file called MyImpl.js
    class MyImpl extends MonoBehaviour implements MyInterface {
        function bar() {
             Debug.Log("Interfaces work!");
        }
    }
    

I know the question is about how to use Interface but here why I do not use them.
Interface cannot inherit from MonoBehaviour so you lose all of the advantages of making it a component.

As a matter of fact you get the exact same behaviour with an total abstract class except the fact of the single inheritance but you can easily get over that.

Fetching an object of type IInterface means you need to use a FindObjectOfType which is not that good.
Using an abstract class you can do GetComponent()

Also, you can actually create a reference in another class and use the drag and drop in the inspector which I never managed to get working with an interface since no MonoBehaviour. I guess a Editor scritp would do.

Nonetheless, I may be wrong and there might be a way to do with Interface what I could not do…Forgive my ignorance.

The problem is that unityscript dont have the keyword abstract so you must use virtual and leave {} empty or with a return and then override it to have the same effect of an abstract but if you just have method to implement you can “inherit” from an interface that is why you would use it. You cannot make a component with an abstract class or an interface by the way and you use find of type because you are actually making a new type right?.