Calling Expression in Strong Mode Immediately

In strict mode, we cannot use expressed expressions with expressed function (IIFE)? The following program proves that I cannot use IIFE in strict mode. If I comment on 'use strict', this works. Is this because each expression in strict mode must have a name?

'use strict'
(function _test () {
var obj = {`enter code here`
        a:      2,
        b:      'name',
        c:      function _c (){
                console.log('a: ' + this.a + " b: "+ this.b);
        }
};
obj.c();
}) ();

Below is the conclusion

(function _test () {
^
TypeError: string is not a function
    at Object.<anonymous> (/home/ganesh/temp/let.js:2:1)
    at Module._compile (module.js:456:26)
    at Object.Module.`enter code here`_extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3
+4
source share
1 answer

The main problem is the lack of a semicolon after use strict. When the JS engine examines the lexical structure of your code, it sees 'use strict'and then (, therefore, it expects a form function name().

One of the rules for automatically including semicolons:

5 :

(, [, +, -, and /

, , , .

enter code here, SO use strict, .

'use strict';
(function _test() {
  var obj = {
    a: 2,
    b: 'name',
    c: function _c() {
      console.log('a: ' + this.a + " b: " + this.b);
    }
  };
  obj.c();
})();
Hide result
+4

Source: https://habr.com/ru/post/1613587/


All Articles