【发布时间】:2015-11-05 16:27:25
【问题描述】:
我有那个代码:
function defineProperty(object, name, callback){
if(object.prototype){
Object.defineProperty(object.prototype, name, {"get": callback});
}
}
defineProperty(String, "isEmpty", function(){return this.length === 0;});
我使用它如下:
console.log("".isEmpty, "abc".isEmpty);
然后它返回:
true, false
现在,我想把函数改成这样:
defineProperty(String, "isEmptyWithArrow", () => this.length === 0);
但是“this”是指Window,我不知道怎么改。
【问题讨论】:
标签: javascript ecmascript-6 arrow-functions