Strict violation when using 'this' in declared function, no violation with function expression

javascript, jslint

Solution

Have a look at this link: https://github.com/shichuan/javascript-patterns/blob/master/general-patterns/function-declarations.html

From this excellent reference here: http://shichuan.github.com/javascript-patterns/

These reasons are provided, but I'm not sure how convincing they are:

- Makes it easier to understand "functions as an object".

- It enforces good semicolon habits.

- Doesn't have much of the baggage traditionally associated with functions and scope.

Reasons for "named function expression" on that page are a little more persuasive, but still not overwhelming.

By the way, those appear to come from John Resig (of jQuery fame), as linked here: http://ejohn.org/blog/javascript-as-a-first-language/

Problem

Why does jslint complain about using this in a declared function? ``` function navigate() { var id = $(this).attr('id'); } ``` Gives me: ``` #1 Strict violation. var id = $(this).attr('id'), // Line 18, Pos 20 ``` Yet jslint gives no complaint about: ``` var navigate = function () { var id = $(this).attr('id'); } ``` I'm using them both in the same way, and they both work correctly in the browser. ``` view.on('click', navigate); ``` FYI, I got around the warning by using `event.target` instead, but I would like to know what the distinction is. ``` function navigate(event) { var id = $(event.target).attr('id'); // no complaint } ```

Original source