well, you can’t have that exactly… anyways it results in a weird syntax in the code.
There is extension methods though… like this:
(this is C#)
public static class ExtensionMethods
{
public static void PrintName(this object obj) { Debug.Log(obj.name); }
}
public class Foo
{
void Start()
{
this.name = "Fart Knocker";
this.PrintName(); //'this' is implicitly passed in, and 'Fart Knocker' is logged
}
}
This can accomplish a similar task for you, allows for a cleaner syntax, and should probably accomplish the same design you’re looking for.
For other fun hacking you should look into ‘anonymous functions’ and ‘reflection’. This again is C#. Not exactly sure what features unityscript/javascript supports. I know it has access to the reflection library, I know it can’t do extension methods, I don’t know about anonymous functions.
You still have to pass in the object… you just have to define the parameter in the function. You think defining the parameter in the function is ugly?
I think it’s ugly that you use the name ‘_this’, but aside from that, having the parameter is not ugly IMO. With the parameter defined you know what is going on. Otherwise reading the code you’re always wondering which ‘this’ ‘this’ is referring to as it’s not actually ‘this’ but instead some non-‘this’ you passed in to a function that isn’t even shaped to accept a ‘this’… UGH.
See ugly is a bad word for this. Ugly implies preference of look. I could think spaghetti code is pretty to look at from an abstract artsy point of view. But from a practical point of view it’s bad code.
What you’re suggesting is bad code. It may be pretty, it may be ugly aesthetically… but programmers will call it ‘ugly’ or ‘smelly’ or ‘code stink’ because it’s BAD CODE. And it’s bad code because it’s not readable.
I would disagree, because the beauty of this particular code comes straight from jQuery’s syntax.
I’m currently helping some of my web developer friends to understand and become more familiar with Unity, so I’m porting basic jQuery functionality to Unity.
That’s kind of what I was pointing out. Beauty is the wrong word to use here.
That is called code stink.
jQuery, though useful, is notorious for its code stink. Javascript in general is known for its code stink. Unityscript follows the ecmascript standard (and is not actually javascript), the ecmascript standard is the standard that jQuery breaks. Unityscript furthermore is an implementation of ecmascript to compile into mono, so it also follows those standards too… which this breaks as well.
And in standard programmer parlance, code stink is a synonym for ‘ugly’.
See javascript w/ jQuery in the end doesn’t really follow any object-oriented standards. It breaks nearly all the rules. But because no one expects object-oriented behavior in javascript, they get away with it. ‘this’ doesn’t mean the same thing there necessarily.
In mono/.net, this means a whole lot more and to remain readable the standards of it must remain standard. Once it becomes unreadable… well that’s ugly. Again, using the word ugly in standard programmer parlance, meaning ‘unreadable’.
So in the end. Answer to your original question. NO, you can not accomplish what you desire, because Unity is not javascript running in a browser breaking rules with jQuery to remain compact.
You don’t have the bandwidth limitations, so all the rule breaking jQuery commits is not justified.
But C# offers other more elegant tools to accomplish similarish results, while not being unreadable.