use-strict

use strict in javascript not working for fat arrow?

江枫思渺然 提交于 2020-07-08 06:12:06
问题 I found an interesting case where "use strict" is not working as expected in javascript. Following functions "use strict"; var y = () => { console.log(this); } var x = function () { console.log(this); } x(); // undefined due to use strict y(); // window object I think fat arrow context should also be overwritten by undefined, or is my assumption wrong? 回答1: MDN says of arrow functions: Relation with strict mode Given that this is lexical, strict mode rules with regard to this are just ignored

Why does JSLint give strict violation error on this function?

∥☆過路亽.° 提交于 2020-01-01 09:33:13
问题 JSLint gives me the "strict violation" error, although I use the "this" context inside a function which hides it from the global scope. function test() { "use strict"; this.a = "b"; } For the record, I use the built-in JSLint parser in Webstorm. 回答1: This is because JSLint doesn't recognize your function as a constructor. By convention, you must use uppercase letters. function Test() { "use strict"; this.a = "b"; } 来源: https://stackoverflow.com/questions/17770048/why-does-jslint-give-strict

use strict leads to undefined function

…衆ロ難τιáo~ 提交于 2019-12-31 05:15:22
问题 I am trying to organize my js file and followed a suggested module pattern. When I use "use-strict" in this pattern a function is declared as undefined, without the "use-strict" method the function just works fine. Is the strict-mode recommended and when yes, why does the function not work with the use of it? var envy = (function( $ ) { 'use strict'; /** * Viewport adjusments. * * @since 1.0.0 */ irp_viewportadjst = function() { $('meta[name="viewport"]').attr('content', 'width=device-width,

How to set 'use strict' globally with JSLint

大憨熊 提交于 2019-12-19 05:44:50
问题 I'm new to javascript and am trying to validate through JSLint. Where should I put "use strict" to use it globally and validate? This gives me error "Unexpected expression 'use strict' in statement position.": "use strict"; console.log('doing js in head-section'); function helloWorld() { console.log('called function helloWorld()'); alert('Hello World from a JS function showing an alert!'); } function helloMyNumber() { console.log('called function helloMyNumber()'); var max = 42; var

Javascript use strict error not catching

天大地大妈咪最大 提交于 2019-12-12 09:53:00
问题 I am creating a backbone.js app that uses require.js for AMD. In order to check for use strict support in the browser, I have included the following code. However, when the code is run, the error thrown by var o = {p:1, P:2} is not caught as I expect it to be, and instead kills the entire page. Chrome console prints this error: Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode require([ 'jquery', 'underscore', 'backbone', 'src/app' ], function(jQuery,

“use strict” inheritance / scope

怎甘沉沦 提交于 2019-12-10 22:28:05
问题 //Global Scope "use strict"; //1 function A() { "use strict"; //2 function innerA() { "use strict"; //3 } } I was just wondering: Is doing use strict at //1 is enough or do we have to be explicit at all places like //2 and //3 . 回答1: Quoting MDN on strict mode, To invoke strict mode for an entire script, put the exact statement "use strict"; (or 'use strict'; ) before any other statements. concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable

How is strict mode (“use strict”;) inherited by functions?

丶灬走出姿态 提交于 2019-12-10 04:23:05
问题 Here is my code that seems to indicate that the answer is yes - http://jsfiddle.net/4nKqu/ var Foo = function() { 'use strict' return { foo: function() { a = 10 alert('a = ' + a) } } }() try { Foo.foo() } catch (e) { alert(e) } Could you please cite the statements from the standard that clarifies that 'use strict' is automatically applied to all closures and functions defined within a function to which we have applied 'use strict' ? 回答1: The relevant part of the spec: http://www.ecma

Is this a Chrome bug or is my use of “use strict” and eval invalid?

纵然是瞬间 提交于 2019-12-10 02:21:50
问题 This code works alerts "ok" in all browsers except Chrome: eval("var outer = 0; function test() {'use strict'; outer = 1; } test(); alert('ok');"); (Try it on jsfiddle ). All I'm doing is referencing an outer variable from a 'use strict' function, all in eval context. Chrome says Uncaught ReferenceError: outer is not defined Note: I originally faced it when using devtool: 'eval' in Webpack. 回答1: To simplify the problem : http://jsfiddle.net/rokkkjcs/6/ eval("var outer=0;"); function test() {

Javascript/jsLint: What to replace jQuery(this) with when using “use strict”;

强颜欢笑 提交于 2019-12-08 14:30:11
问题 When I validate the following code with jslint I get the following errors. function displayMegaDropDown() { "use strict"; var liMegaPosition, divMegaOffset; liMegaPosition = jQuery(this).position(); divMegaOffset = { top: liMegaPosition.top + jQuery(this).height(), left: liMegaPosition.left }; jQuery(this).find("div").offset(divMegaOffset); jQuery(this).addClass("hovering"); } Problem at line 4 character 29: Strict violation. liMegaPosition = jQuery(this).position(); Problem at line 5

Javascript: besides “use strict”, which other “use” directives are there?

我怕爱的太早我们不能终老 提交于 2019-12-06 02:36:55
问题 Besides use strict , which other use directives are there? 回答1: Some more examples that can be in the “directive prologue” (a section potentially usable by JavaScript engines): 'use strict'; 'use asm'; Mozilla's asm.js is a subset of the language, geared towards crunching numbers. 'use stricter'; Google's SoundScript. For fast OOP Has also some modes like: 'use stricter+types'; http://www.2ality.com/2015/02/soundscript.html 'use babel'; Used in Atom.io. (Was previously: 'use 6to5'; ) A tweet