javascript - Why is this illegal in strict mode? -


yeah, yeah, know, strict mode ain't around yet, really, i'm planning future...

so, why this:

$('#'+ $(this).attr('id').replace('control-', 'legend-')).fadein(); 

... not allowed in es5 strict mode?

or misinterpreting? jslint:

problem @ line 516 character 18: strict violation.

could little more verbose, wonder...?

edit:

to avoid confusion, here's more of original code:

function displaylegend() {     $('#'+ $(this).attr('id').replace('control-', 'legend-')).fadein(); } 

some trial-and-error of code in jslint

"use strict"; var that="dd"; function $(x){return x;}  $('#'+ $(this).attr('id').replace('control-', 'legend-')).fadein(); $(this); 

shows me what's wrong: you're using this parameter. changing both thises thats doesn't trigger error.

as the specification says:

if this evaluated within strict mode code, this value not coerced object. this value of null or undefined not converted global object , primitive values not converted wrapper objects. the this value passed via function call (including calls made using function.prototype.apply , function.prototype.call) not coerce passed this value object (10.4.3, 11.1.1, 15.3.4.3, 15.3.4.4). [my emphasis]

as john resig writes,

finally, long-standing (and annoying) bug has been resolved: cases null or undefined coerced becoming global object. strict mode prevents happening , throws exception instead.

(function(){ ... }).call( null ); // exception 

as showed, using line of code inside function declaration throws error in jslint, whereas using inside function expression doesn't. looks jslint erroneously parses function declaration, sees this, that's still undefined @ moment, , throws exception.

at point, think have quote juriy zaytsev (‘kangax’):

does matter?

it’s understand strict mode not requirement, merely option. there provide stricter rules need it, , willing cope (and enjoy) consequences.


update: @ last found explanation. if read this thread, message #1512 onwards, you'll read that

the point of es5/strict prohibit leaking of global object, es3 promiscuously. es5/strict of work dynamically, , of work statically. jslint of work statically, must more restrictive in order best program right. [douglas crockford in #1553]

i have admit has valid point here: if goal avoid global namespace pollution, shouldn't use function declarations, function expressions inside private namespace, anyway. agree others in mentioned thread error message should more explicit (and throw warning on encountering function declaration).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -