diff --git a/src/api/loc.js b/src/api/loc.js index 0a8ce222ea..4c36d11535 100644 --- a/src/api/loc.js +++ b/src/api/loc.js @@ -22,11 +22,20 @@ ol.loc = function(opt_arg){ return opt_arg; } - var x = 0; - var y = 0; + /** @type {number|undefined} */ + var x; + + /** @type {number|undefined} */ + var y; + + /** @type {number|undefined} */ var z; + + /** @type {Object|undefined} */ var projection; + var usage = 'ol.loc accepts a coordinate array or an object with x, y, and (optional) z properties'; + if (arguments.length == 1 && goog.isDef(opt_arg)) { if (goog.isArray(opt_arg)) { x = opt_arg[0]; @@ -39,9 +48,13 @@ ol.loc = function(opt_arg){ z = opt_arg['z']; projection = opt_arg['projection']; } else { - throw new Error('ol.loc'); + throw new Error(usage); } } + + if (!goog.isNumber(x) || !goog.isNumber(y)) { + throw new Error(usage); + } if (goog.isDef(projection)) { projection = ol.projection(projection); diff --git a/test/spec/api/loc.test.js b/test/spec/api/loc.test.js index a7bbf71168..0296f15ca4 100644 --- a/test/spec/api/loc.test.js +++ b/test/spec/api/loc.test.js @@ -1,11 +1,12 @@ describe("ol.loc", function() { - it("allows empty construction", function() { - var loc; + it("doesn't allow empty construction", function() { + + expect(function() { + // nowhere + var loc = ol.loc(); + }).toThrow(); - // nowhere - loc = ol.loc(); - expect(loc).toBeA(ol.Loc); }); it("allows construction from an obj config", function() {