use-strict

Why is “this” in an anonymous function undefined when using strict?

99封情书 提交于 2019-11-26 10:21:05
Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn't find any concrete answer. Example: (function () { "use strict"; this.foo = "bar"; // *this* is undefined, why? }()); Test in a fiddle: http://jsfiddle.net/Pyr5g/1/ Check out the logger (firebug). It's because, until ECMAscript 262 edition 5, there was a big confusion if people who where using the constructor pattern , forgot to use the new keyword. If you forgot to use new when calling a constructor function in ES3, this referenced the global object (

Why is “this” in an anonymous function undefined when using strict?

十年热恋 提交于 2019-11-26 02:07:03
问题 Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn\'t find any concrete answer. Example: (function () { \"use strict\"; this.foo = \"bar\"; // *this* is undefined, why? }()); Test in a fiddle: http://jsfiddle.net/Pyr5g/1/ Check out the logger (firebug). 回答1: It's because, until ECMAscript 262 edition 5, there was a big confusion if people who where using the constructor pattern , forgot to use the new

What does “use strict” do in JavaScript, and what is the reasoning behind it?

风格不统一 提交于 2019-11-25 21:42:49
问题 Recently, I ran some of my JavaScript code through Crockford\'s JSLint, and it gave the following error: Problem at line 1 character 1: Missing \"use strict\" statement. Doing some searching, I realized that some people add \"use strict\"; into their JavaScript code. Once I added the statement, the error stopped appearing. Unfortunately, Google did not reveal much of the history behind this string statement. Certainly it must have something to do with how the JavaScript is interpreted by the