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地图)_Javascript_Google Maps_Class_Object - Fatal编程技术网

Javascript 将类实例设置为对象(google地图)

Javascript 将类实例设置为对象(google地图),javascript,google-maps,class,object,Javascript,Google Maps,Class,Object,我试图在我的网站上使用谷歌地图。我决定创建类“map”,这样我就可以使用类方法和对象 问题是:我想让类构造函数知道这个类的每个实例都自动是一个googlemap。 所以当我打电话时: map = new map(location); 我会得到一张谷歌地图,“这个”会指的是那张地图。这是我的构造器: map.prototype = new Element(); map.prototype.constructor = map; function map (place) { this.do

我试图在我的网站上使用谷歌地图。我决定创建类“map”,这样我就可以使用类方法和对象

问题是:我想让类构造函数知道这个类的每个实例都自动是一个googlemap。 所以当我打电话时:

map = new map(location);
我会得到一张谷歌地图,“这个”会指的是那张地图。这是我的构造器:

map.prototype = new Element();
map.prototype.constructor = map;

function map (place) {
    this.dom = document.createElement('div');
    document.body.appendChild(this.dom);

    // here I am trying to set "this" to the google map
    // option 1: obviously wrong
    // this = new google.maps.Map(this.dom, {zoom: 9, center: place.location});

    // option 2: overwrites dom, but I want dom to be the div
    // this.dom = new google.maps.Map(this.dom, {zoom: 9, center: place.location});

    //option 3: this works, I don't want to have this extra sub-step using this."map".something...
    this.map = new google.maps.Map(this.dom, {zoom: 9, center: place.location});         


    // call other functions, etc...
};

有没有办法做得更好?无需创建另一个对象属性并将映射放入其中?

您可以使用新的类语法使其更具可读性:

class Map extends google.maps.Map {
  constructor(){
     super(document.body.appendChild(document.createElement('div')), {zoom: 9, center: place.location});

  }

  customMethod(){
     this.whatever();
  }
}

您可以使用新的类语法使其更具可读性:

class Map extends google.maps.Map {
  constructor(){
     super(document.body.appendChild(document.createElement('div')), {zoom: 9, center: place.location});

  }

  customMethod(){
     this.whatever();
  }
}

如果我正确理解了您的方法,它将使我的map类继承google.maps.map类的所有内容。但是,它本身并不是谷歌地图。对吗?这个方法和我的方法(选项3)有什么不同吗?另外,我得到一个错误:uncaughtreferenceerror:google不是defined@jan是的,因为它继承了一切,它本身就是谷歌地图如果我正确理解你的方法,它会使我的地图类继承google.maps.map类的一切。但是,它本身并不是谷歌地图。对吗?这个方法和我的方法(选项3)有什么不同吗?另外,我得到一个错误:uncaughtreferenceerror:google不是defined@jan是的,因为它继承了一切,它本身就是谷歌地图