Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of thisArg in .forEach?

JavaScript's forEach documentation states that the .forEach syntax is:

arr.forEach(callback[, thisArg])

What is the usage of thisArg?

like image 612
asdf_enel_hak Avatar asked Jan 01 '16 13:01

asdf_enel_hak


1 Answers

thisArg refers to context which callback should be called, basically it is what this refers to inside callback. For example:

var myObject = { name: 'myObject' };

[1,2].forEach(function(item) { 
 console.log(item); // 1, 2
 console.log(this === myObject); // true
}, myObject)
like image 172
Bek Avatar answered Oct 31 '22 20:10

Bek