Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 匿名回调中的作用域注意事项_Javascript_Jquery - Fatal编程技术网

Javascript 匿名回调中的作用域注意事项

Javascript 匿名回调中的作用域注意事项,javascript,jquery,Javascript,Jquery,我把基本知识放错地方了,有一个简单的范围问题:我正在通过JQuery事件侦听器运行一系列对不同分析库的函数调用: $('a').filter(function() { return this.href.match(/twitter\.|facebook\.|instagram\./) }) .on('click', function() { // What's faster? Caching the property of `this`: var t = this.title;

我把基本知识放错地方了,有一个简单的范围问题:我正在通过JQuery事件侦听器运行一系列对不同分析库的函数调用:

$('a').filter(function() {
  return this.href.match(/twitter\.|facebook\.|instagram\./)
})
.on('click', function() {
  // What's faster? Caching the property of `this`:
  var t = this.title;
  fbq('track', 'Lead', { content_name: t, content_category: 'Social Link' });
  ga('send', 'event', 'Social Link', 'click', t);


  // Or just call `this.title` directly?
  fbq('track', 'Lead', { content_name: this.title, content_category: 'Social Link' });
  ga('send', 'event', 'Social Link', 'click', this.title);
});

我有一部分担心的是,对
这个
的引用会导致范围的扩大和速度的减慢。有人愿意解释我哪里出了错吗?

我对其他人对此的看法很感兴趣。我倾向于优先考虑代码的可读性,而不是这些小的性能改进。除非你在做成千上万个这样的调用,否则我不会期望有任何明显的区别。我不确定,但我猜var是最快的,因为否则代码将不得不找出它引用了什么