2017-08-09 29 views
0

我正在使用聚合物2,我正在使用mixin进行行为。在我的行为子类中,我无法使用相同子类的方法。我怎样才能做到这一点? 这里是我的代码:如何在自我类中使用子类函数

const Generic = (subclass) => class extends subclass 
{ 
constructor() 
{ 
    super(); 
} 

_arrayIntersect (a, b) 
{ 
let bigArray = a.length > b.length ? a : b, common = []; 

bigArray.forEach(function (elm) { 

    if(a.indexOf(elm) !== -1 && b.indexOf(elm) !== -1) 
    { 
    common.push(elm); 
    } 
}); 

return common; 
} 

_inArray (needle, haystack) 
{ 
    let length = haystack.length; 
    for(let i = 0; i < length; i++) 
    { 
    if(haystack[i] === needle) return true; 
    } 
return false; 
} 

bodyClick() 
{ 
    el.addEventListener('click', function(e) { 
    // How to use `_arrayIntersect` and `_inArray` from here 
    // this._inArray(needle, haystack) getting undefined message 
    }); 
} 
}; 

回答

0

单击事件内的this对象指示点击的元素,而不是类的实例。要调用超/子类的方法,你可以指定this对象的变种,像下面这样:

bodyClick() 
{ 
    let self = this; 
    el.addEventListener('click', function(e) { 
     let isExist = self._inArray(needle, haystack); 
    }); 
} 
相关问题