Getter and Setter + JS

Given a JS file JSClass, why this works:

class JSClass extends MonoBehaviour
{
    var x: String = '';
    function get X() : String { return x; }
    function set X(value : String) { x = value; }
}

And this dont work:

var x: String = '';
function get X() : String { return x; }
function set X(value : String) { x = value; }

Because UnityScript is not true javascript.

You can implement similar behavior though, see this link:

http://www.unifycommunity.com/wiki/index.php?title=ExpandoObject

I think you missed the point; the first code example works fine. The question is why you have to explicitly declare the class in order for the getter/setter functionality to work. The answer is: because. :wink: I have no idea.

–Eric

lol eric , usually yer comments are quite tech oriented m i enjoyed that.

Yeah, this looks weird to me too. Thanks Eric.

I think it just works declaring the class because for getters and setters to work you need a class instance.
Eg.

var myClass:MyClass = new MyClass();
myClass.value = v;

And you can’t have an instance wihtout creating the class and it’s constructor function, I guess.