[TxMt] Better JavaScript functions

Gavin Kistner gavin at refinery.com
Sun Oct 9 14:11:31 UTC 2005


On Oct 9, 2005, at 3:56 AM, Andreas Wahlin wrote:
> The effects were mainly that of all the possible ways for  
> JavaScript to declare functions, covered in part by Gavin here
> http://phrogz.net/JS/Classes/OOPinJS.html
>
> I believe all are
>
> function funcName(arg) { .. }
> this.funcName = function(arg) { ... };
> var funcName = function(arg) { ... };
> object.prototype.funcName = function(arg) { ... };
>
> and then instead of
> function(arg) { ... };
> you can have
> new Function("arg", "...");
> so for example this.funcName = new Function("arg1", "arg2", "alert 
> (arg1+arg2);");
>
> If anyone could confirm this it would be quite nice :)

Those are all correct; slightly missing is the fact that any of the  
function literals (using the function keyword) can be followed by  
funcName, and a couple other ways you could create a named function  
(anywhere you can assign a value to a variable, you can use a  
function literal). So, in addition to the above:

this.funcName = function funcLabel ( args ) { ... }
var functName = function funcLabel ( args ) { ... }

obj[ 'funcName' ] = function ( args ) { ... }
obj[ 'funcName' ] = function funcLabel ( args ) { ... }

var foo = { count : 1, toString : function( ) { return this.count } }
var foo = { count : 1, toString : function funcName ( ) { return  
this.count } }
var foo = [ function ( ) { ... }, function ( ) { ... }, function ( )  
{ ... } ]
var foo = [ function foo ( ) { ... }, function bar ( ) { ... },  
function jammy ( ) { ... } ]

... and all the above with the new Function constructor as well.

Though it would be possible to account for all of the above  
explicitly, if *I* were writing the syntax highlighting regexp, I  
think I'd be lazy and simply do:

         {    name = 'meta.function.js';
             match = '\b(function)(\s+[a-zA-Z_]\w*)?\s*\((.*?)\)';
             captures =
             {    1 = { name = 'storage.type.function.js'; };
                 2 = { name = 'entity.name.function.js'; };
                 3 = { name = 'variable.parameter.function.js'; };
             };
         },

and leave all the numerous ways that you can store pointers to this  
object without the benefit of an explicit "entity.name.function.js"  
label for the variable. (And leave Function to be handled under the  
current support.function.js).



More information about the textmate mailing list