Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查这是否是JavaScript中新的google.maps.LatLng的实例_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

检查这是否是JavaScript中新的google.maps.LatLng的实例

检查这是否是JavaScript中新的google.maps.LatLng的实例,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有我的JS对象 function Foo() { this.bar = new google.maps.LatLng(12, 34); } 我还做了一个能手和二传手: Foo.prototype.SetBar = function(bar_arg) { this.bar = bar_arg; } 及 如何强制用户只提供google.maps.LatLng对象作为bar_arg 换句话说,如果用户将调用this.setBar(“我是一个字符串”)而不是this.setBar

我有我的JS对象

function Foo() { 
    this.bar = new google.maps.LatLng(12, 34);
}
我还做了一个能手和二传手:

Foo.prototype.SetBar = function(bar_arg) {
    this.bar = bar_arg;
}

如何强制用户只提供
google.maps.LatLng
对象作为
bar_arg

换句话说,如果用户将调用
this.setBar(“我是一个字符串”)
而不是
this.setBar(新的google.maps.LatLng(56,78))
我想抛出一个异常来执行特定操作。是个好主意吗?如果是,我应该如何检查

if(!bar_arg instanceof google.maps.LatLng){//throw new…}

或者只是

if(!bar_arg instanceof LatLng){//throw new…}


显然Google Maps API已经加载。

您使用
instanceof
的方法是正确的

事实上,对您的尝试稍加修改:

if (!(bar_arg instanceof google.maps.LatLng)) { //throw new... }

应该正是你需要的…

看来你错了。这很奇怪,因为在这种情况下,
not
操作符不起作用。如果我要删除
和add
}else{//在那里我可以抛出}
然后它在
else
中抛出异常。然后你可能需要添加
()
,我已经编辑了我的答案。是的!那确实有效。非常感谢。对不起,我今天累了。您如何处理Google Maps API现在接受的,即像
{lat:12,lng:34}
这样的简单Javascript结构?
if (!(bar_arg instanceof google.maps.LatLng)) { //throw new... }