Javascript 如何使用此关键字从方法访问类属性

Javascript 如何使用此关键字从方法访问类属性,javascript,leaflet,Javascript,Leaflet,我正在尝试为传单地图查看器开发一个插件。我有一个带有选项属性列表的类,但当我试图从onAdd访问它们时,使用此键的方法无效 L.btsControl = L.Control.extend({ options: { position: 'topright', //control position - allowed: 'topleft', 'topright', //'bottomleft', 'bottomright' minValue: 'isss',

我正在尝试为传单地图查看器开发一个插件。我有一个带有选项属性列表的类,但当我试图从
onAdd
访问它们时,使用此键的方法无效

L.btsControl = L.Control.extend({
options: {
    position: 'topright', 
    //control position - allowed: 'topleft', 'topright',
    //'bottomleft', 'bottomright'
    minValue: 'isss',
    min: 0,
    max: 1,
    maxValue: '',
},

initialize: function (options) {
    L.Util.setOptions(this, options);
},

onAdd: function (map) {
    this._map = map;
    this._map.eachLayer(function (layer){
        if (layer.options.time_i > this.options.max) {
            this.options.maxValue = layer.options.time;
        }
        if (layer.options.time_i < this.options.min) {
            this.options.minValue = layer.options.time;
        }
    });

//SKIPED CODE

    return container;
 },
L.btsControl=L.Control.extend({
选项:{
位置:'topright',
//控制位置-允许:'topleft'、'topright',
//'bottomleft'、'bottomright'
minValue:'isss',
分:0,,
最高:1,
最大值:“”,
},
初始化:函数(选项){
L.Util.setOptions(本,选项);
},
onAdd:函数(映射){
这个._map=map;
此.\u映射每个层(函数(层){
if(layer.options.time_i>this.options.max){
this.options.maxValue=layer.options.time;
}
if(layer.options.time_i
那么,我如何从
onAdd
方法访问最小和最大属性呢?现在我得到

“TypeError:此.options未定义”


调用函数是:
var mycontrol=new L.btscocontrol().addTo(map);
您提供给
.eachLayer()
的回调函数没有所需的上下文。您可以通过将该函数绑定到
this
来解决此问题:

this._map.eachLayer(function (layer){
    if (layer.options.time_i > this.options.max) {
        this.options.maxValue = layer.options.time;
    }
    if (layer.options.time_i < this.options.min) {
        this.options.minValue = layer.options.time;
    }
}.bind(this));
this.\u map.eachLayer(函数(层){
if(layer.options.time_i>this.options.max){
this.options.maxValue=layer.options.time;
}
if(layer.options.time_i

请参阅。

我编辑了问题请拼写检查您的帖子和标题。