This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Foo( int a, int b, int c, int d, int e){ | |
return (a + b + c + d) / e; | |
} | |
void Foo ( int a, int b ){ | |
return Foo(a, b, 0, 0, 1); | |
} |
This example is unnecessary in C#, since the language supports the following:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Foo( int a, int b, int c = 0, int d = 0, int e = 1){ | |
return (a + b + c + d)/e; | |
} |
There are a couple of ways I've found to do this in Javascript, neither of which is particularly appealing:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Foo( args ){ | |
var a = 0; | |
var b = 0; | |
var c = 0; | |
var d = 0; | |
var e = 1; | |
if ( args['a'] ) a = args['a']; | |
if ( args['b'] ) b = args['b']; | |
if ( args['c'] ) c = args['c']; | |
if ( args['d'] ) d = args['d']; | |
if ( args['e'] ) e = args['e']; | |
return (a + b + c + d)/e; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Foo( a, b, c, d, e ){ | |
a = typeof a !== 'undefined' ? a : 0; | |
b = typeof b !== 'undefined' ? b : 0; | |
c = typeof c !== 'undefined' ? c : 0; | |
d = typeof d !== 'undefined' ? d : 0; | |
e = typeof e !== 'undefined' ? e : 1; | |
return (a + b + c + d ) / e; | |
} |
Now, it may be possible to use the C# style syntax if one uses CoffeeScript or TypeScript; I haven't done enough research to know. But it would be wonderful if javascript supported that syntax natively, just to reduce the amount of boiler-plate that you have to use. Boilerplate is almost always bad; it introduces the possibility of simple mistakes.
No comments :
Post a Comment