Why "var X = function X() {};"

I have noticed this pattern in several files:

var CesiumTerrainProvider = function CesiumTerrainProvider(description) { ... } ;

Is this expected, or should it be one of:

var CesiumTerrainProvider = function(description) { ... };

function CesiumTerrainProvider(description) { ... };

I haven’t found anything in the CC about this. Is this should be changed please tell me which form to use and I’ll submit a PR.

Cheers,

Victor

Hi Victor,

Welcome back. :slight_smile:

I had the same question myself at one point, and I thought there was a reason for it. It might be doc related. Anyone remember?

Patrick

No, there’s no reason for it. At one point all of these functions were declared like “function foo()”. In order to show up in the documentation, they need to be declared as “var foo = function()”. Whomever went through and updated everything ended up changing them to “var foo = function foo()” which still works, but is overly verbose. Taking them out is a good change.

The PR I submitted yesterday takes care of that in the way Matthew
describes, see
https://github.com/AnalyticalGraphicsInc/cesium/pull/669

Cheers,
Vic

This was my doing, and there’s a reason.

function Foo() {}

won’t show up in the documentation.

var Foo = function() {}

Has the call stack problem Victor mentioned, and for constructors, the constructor.name property is blank. This was a problem for me during testing. For example, see ‘toConformToInterface’ in addDefaultMatchers.js. With the style I used, helpful messages are printed when, for example, CesiumTerrainProvider doesn’t conform to the TerrainProvider interface. If you change the function signature these messages will no longer be useful.

The ideal solution, I think, would be to fix the doc tool to support the first syntax.

Kevin

The ideal solution, I think, would be to fix the doc tool to support the first syntax.

Sounds like a good issue to submit with the GSoC label as a way for folks to get started even though it isn’t Cesium proper.

Patrick

Done:

https://github.com/AnalyticalGraphicsInc/cesium/issues/676