[Enhancement] Construction pattern

Dear all:

The Cesium code often uses the following pattern:

var Cartographic = function(longitude, latitude, height) {

this.longitude = defaultValue(longitude, 0.0);

this.latitude = defaultValue(latitude, 0.0);

this.height = defaultValue(height, 0.0);


Cartographic.fromDegrees = function(longitude, latitude, height**, result**) {

    longitude = CesiumMath.toRadians(defaultValue(longitude, 0.0));

    latitude = CesiumMath.toRadians(defaultValue(latitude, 0.0));

    height = defaultValue(height, 0.0);

    if (typeof result === 'undefined') {

return new Cartographic(longitude, latitude, height);


result.longitude = longitude;

result.latitude = latitude;

result.height = height;

return result;


I think it is not that good because of:

  • code duplication,

  • not really OOP (result as a parameter).

A better pattern would be:

carto = new Cartographic();

carto.fromDegrees(…); // OO

It could require a lot of small changes in the code base but I can volunteer to implement if you feel it’s valuable.



Our general rule for constructors is that it should take as parameters the internal or public properties that best represent it; this allows for the most efficient constructor possible. For example, Cartographic takes lon/lat/alt as radians and meters because that’s how it is stored internally. The reason we don’t require an instance for construction is because it makes the code unnecessarily verbose. As an aside, the result parameters are a necessity to facilitate object reuse. Construction in general is slow and reuse is a must in order to avoid excessive garbage collection in such a large and complex application like Cesium.

This all being said, I’m not against adding prototype versions of the from functions, but I would not remove any existing code. So in your example, I would simply add a fromDegrees that calls the static version:

Cartographic.prototype.fromDegrees = function(longitude, latitude, height) {

return Cartographic.fromDegrees(longitude, latitude, height, result);


I’d like to hear from a few other people what their thoughts are, if people think having prototype version of the from functions are a good idea, then we can do it, but if there’s something I’m missing that someone points out, or no one really cares, then I’m not sure it’s worth it.