检查这是否是JavaScript中new google.maps.LatLng的实例
Check if this is an instance of new google.maps.LatLng in JavaScript
我有我的JS对象。
function Foo() {
this.bar = new google.maps.LatLng(12, 34);
}
我还做了一个getter和setter:
Foo.prototype.SetBar = function(bar_arg) {
this.bar = bar_arg;
}
和
Foo.prototype.GetBar = function() {
return this.bar;
};
如何强制用户只提供google.maps.LatLng
对象作为bar_arg
?
换句话说,如果用户将调用this.setBar('i am a string')
而不是this.setBar(new 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... }
相关文章:
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 应用“;矢量“;指向传单中的Point或LatLng
- 使用Google Maps API向标记添加多个字符
- Google Maps API OverlayView()在AngularJS指令中不起作用
- 使用Google Maps API驱动时间多边形
- 使用Google Maps JavaScript API v3和Geocoding API映射多个位置
- 将谷歌地图点击事件中的LatLng传递到rails控制器
- 将 jquery .position() 转换为 google maps latlng
- Google Maps APIv3 large polygon new google.maps.LatLng redun
- GWT Google Maps:setCenter:不是LatLng或LatLngLiteral:属性lat:不是数字
- 检查这是否是JavaScript中new google.maps.LatLng的实例
- 将值传递给google.maps.LatLng
- 如何使用变量来定义新的google.maps.LatLng()
- 不能使用google.maps.LatLng对象's late
- 谷歌地图地理编码器如何我可以返回值在新的Google .maps. latlng
- Google Maps JavaScript将变量放入LatLng()中
- 在Google Maps API V3中使用LatLng坐标数组绘制折线
- 将JSON字符串值分配给MVC 4中的google.maps.LatLng(lat,lon)中的Latitude和Lon
- 谷歌地图API v3-类型错误:表达式的结果'google.maps.LatLng'[defined]不
- TypeError: google.maps.latLng不是构造函数