Javascript 从参数扩展类
在理想的情况下,我将能够导入Javascript 从参数扩展类,javascript,class,ecmascript-6,Javascript,Class,Ecmascript 6,在理想的情况下,我将能够导入google,并对其进行扩展,同时在我的类中使用它 import google from 'google' class GoogleMapsPopover extends google.maps.OverlayView{ constructor ({point, map}) { this.bounds_ = new google.maps.LatLngBounds(point, point) this.map_ = map this.di
google
,并对其进行扩展,同时在我的类中使用它
import google from 'google'
class GoogleMapsPopover extends google.maps.OverlayView{
constructor ({point, map}) {
this.bounds_ = new google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
但是,我想将google
作为参数传递给googlemapspover
类
如果不进行如下扩展,就无法拥有super
:
class GoogleMapsPopover {
constructor ({google, point, map}) {
super(google.maps.OverlayView)
this.google = google
this.bounds_ = new this.google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
class GoogleMapsPopover {
constructor ({google, point, map}) {
this = google.maps.OverlayView
this.google = google
this.bounds_ = new this.google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
export const getGoogleMapsPopoverClass = (google) => {
return class GoogleMapsPopover extends google.maps.OverlayView {
constructor ({point, map}) {
super()
this.bounds_ = new google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
}
const GoogleMapsPopover = getGoogleMapsPopoverClass(google)
您不能像这样设置this
的值:
class GoogleMapsPopover {
constructor ({google, point, map}) {
super(google.maps.OverlayView)
this.google = google
this.bounds_ = new this.google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
class GoogleMapsPopover {
constructor ({google, point, map}) {
this = google.maps.OverlayView
this.google = google
this.bounds_ = new this.google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
export const getGoogleMapsPopoverClass = (google) => {
return class GoogleMapsPopover extends google.maps.OverlayView {
constructor ({point, map}) {
super()
this.bounds_ = new google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
}
const GoogleMapsPopover = getGoogleMapsPopoverClass(google)
我能想到的唯一替代方法是将类包装在函数中,这有点粗糙
大概是这样的:
class GoogleMapsPopover {
constructor ({google, point, map}) {
super(google.maps.OverlayView)
this.google = google
this.bounds_ = new this.google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
class GoogleMapsPopover {
constructor ({google, point, map}) {
this = google.maps.OverlayView
this.google = google
this.bounds_ = new this.google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
export const getGoogleMapsPopoverClass = (google) => {
return class GoogleMapsPopover extends google.maps.OverlayView {
constructor ({point, map}) {
super()
this.bounds_ = new google.maps.LatLngBounds(point, point)
this.map_ = map
this.div_ = null
this.setMap(map)
}
}
}
const GoogleMapsPopover = getGoogleMapsPopoverClass(google)
JS“类”看起来不太方便。为什么不使用
Object.create
?@evolutionxbox你能举个例子吗?到底是什么阻止你在理想世界中实现它?为什么你会“想把谷歌作为一个论据传进来”?不,将超类作为参数传递给构造函数是没有意义的。JS“classes”看起来不太方便。为什么不使用Object.create
?@evolutionxbox你能举个例子吗?到底是什么阻止你在理想世界中实现它?为什么你会“想把谷歌作为一个论据传进来”?不,将超类作为参数传递给构造函数是没有意义的。