x


Why does this run it's "super" method?

Here's a simplified example to explain my problem in Javascript:

I have a super-class:

class mySuperClass()
{
static function run()
{
return "mySuperClass";
}
}

And a sub-class which inherits from my super-class:

class mySubClass extends mySuperClass
{
static function run()
{
return "mySubClass";
}
}

And finally, a 3rd class:

class doThings
{
function Start()
{
var aClass = new mySubClass();
}
function Update()
{
Debug.Log(aClass); //This displays the text 'mySubClass'
var myText = aClass.run();
}
}

1) How comes myText equals "mySuperClass"? Surely it should run the mySubClass.run() method instead of the mySuperClass.run() method?

2) Is there any way to make it so that the super-class can never be instantiated (I know interface and abstract isn't supported)?

more ▼

asked Apr 22 '10 at 11:02 PM

joeltebbett gravatar image

joeltebbett
148 14 14 23

It worked ok for me, but I had to adjust it slightly to get it to compile. I realize you have simplified it for the example, but it may have lost something in translation.

class mySuperClass { static function run() { return "mySuperClass run"; } }

class mySubClass extends mySuperClass { static function run() { return "mySubClass run"; } }

var aClass;

function Start() { aClass = new mySubClass(); } function Update() { Debug.Log(aClass); //This displays the text 'mySubClass' var myText = aClass.run(); Debug.Log("myText = " + myText); }

Apr 22 '10 at 11:46 PM Molix

Thanks for your time Molix. I've managed to solve the problem ... I don't think the example I gave actually had the problem I was having in my actual code. Making my superclass functions Virtual solved the problem. Again, thanks for the effort!

Apr 23 '10 at 12:22 AM joeltebbett
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I appear to have solved this myself.

I needed to make the functions in my superclass 'virtual' functions ... for some reason I thought virtual wasn't supported in Javascript, and thus I am a fool ;)

more ▼

answered Apr 23 '10 at 12:19 AM

joeltebbett gravatar image

joeltebbett
148 14 14 23

The Unity implementation of JavaScript isn't actually JavaScript. It's more... just the syntax of JavaScript adopted to .NET/Mono. It's a pretty poor implementation, too, if you ask me. C# is a whole lot easier to understand and code with in the long run.

Apr 23 '10 at 02:43 AM qJake

If we could vote up comments, I would vote up SpikeX's comment. Mono JS is just enough like your browser's JS to be annoying (and too different to be useful as a "javascript implementation " IMHO). If you are doing anything serious you should seriously consider using C#. It's more structured, but like Spike said, that buys you a lot in the long run.

Apr 23 '10 at 03:11 PM raypendergraph

You can vote up comments! Just hover your mouse to the left side of the comment and you have an up arrow you can press to vote for the comment.

Apr 24 '10 at 08:52 AM ericksson
(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

By RSS:

Answers

Answers and Comments

Topics:

x138
x55
x7

asked: Apr 22 '10 at 11:02 PM

Seen: 1631 times

Last Updated: Apr 22 '10 at 11:33 PM