gpt4 book ai didi

javascript - 这个简写更聪明

转载 作者:行者123 更新时间:2023-12-02 17:28:11 24 4
gpt4 key购买 nike

我在两种情况下声明 var that = this 并且我想知道是否有一种更智能的方法可以使用像 call() 这样的 native 函数来处理它apply() 这样我就不必创建匿名函数?

var that = this;
this.listenTo( foo, 'event', function() {
that.trigger( 'change' );
});

var that = this;
this.listenTo( foo, 'event', function( bar ) {
that.add( bar );
});

最佳答案

Function#bind()

所以像这样:

this.listenTo( foo, 'event', function() {
this.trigger( 'change' );
}.bind(this));

this.listenTo( foo, 'event', function( bar ) {
this.add( bar );
}.bind(this));

关于javascript - 这个简写更聪明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23321313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com