关于ECMAScript 6粗箭头或扩展jQuery原型存在很多问题,但我发现没有一个问题可以说明它们如何与示例一起使用。
不使用粗箭头,我通常这样做是为了创建一个用于jQuery的新函数,例如:
$.fn.populate = function (items /*read from .json*/) {
console.log(items); //JS object passed as param
console.log(self); //Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
console.log(this); //jQuery object with $('#populate-me')
$this = $(this);
Object.entries(items).forEach(function (pair) {
$this.append('<div>' + pair[0] + ' - ' + pair[1] + '</div>');
});
};
//usage
$('#populate-me').populate(items);
有没有办法用粗箭头做到这一点?像这样说:
$.fn.populate = (items /*read from .json*/, maybe) => {
console.log(items); //JS object passed as param
console.log(maybe); //undefined
console.log(self); //Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
console.log(this); //Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
$this = $(SOMETHING TO ACCESS #populate-me);
Object.entries(items).forEach(function (pair) {
$this.append('<div>' + pair[0] + ' - ' + pair[1] + '</div>');
});
};
还是应该坚持使用非胖箭头功能?
感谢