最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'use strict' not stopping hoisting in function scope - Stack Overflow

programmeradmin1浏览0评论

My Problem Lies here I'm learning JavaScript But not new to Programming at all. I understand hoisting, but with strict mode shouldn't this produce an error and be caught either when 6 is assigned to undeclared a variable or document.getElement... is assigned x this doesn't produce an error so my diagnosis is that hoisting is still going on..which i don't like and want to get rid of with using strict. Using Chrome Version 42.0.2311.152 m as my browser

function strictMode(){
    'use strict';
    try {
        x = 6;
        document.getElementById('hoisting').innerHTML = x;
        var x;
     }
     catch(err) {
                    document.getElementById('error_report').innerHTML = 
                        "There was an error that occured (Were in Strict Mode)" +
                            " " + err.message;
               }
}

My Problem Lies here I'm learning JavaScript But not new to Programming at all. I understand hoisting, but with strict mode shouldn't this produce an error and be caught either when 6 is assigned to undeclared a variable or document.getElement... is assigned x this doesn't produce an error so my diagnosis is that hoisting is still going on..which i don't like and want to get rid of with using strict. Using Chrome Version 42.0.2311.152 m as my browser

function strictMode(){
    'use strict';
    try {
        x = 6;
        document.getElementById('hoisting').innerHTML = x;
        var x;
     }
     catch(err) {
                    document.getElementById('error_report').innerHTML = 
                        "There was an error that occured (Were in Strict Mode)" +
                            " " + err.message;
               }
}
Share Improve this question asked May 31, 2015 at 15:55 Zach HutchinsZach Hutchins 1031 silver badge4 bronze badges 2
  • 1 I don't think strict mode intend to remove hoisting – GillesC Commented May 31, 2015 at 16:01
  • I read that it did, Which is why it spawned my question...I read that from various websites – Zach Hutchins Commented May 31, 2015 at 17:18
Add a comment  | 

2 Answers 2

Reset to default 15

Variable declarations (i.e. var x;) are valid for the entire scope they are written in, even if you declare after you assign. This is what is meant by "hoisting": the var x; is hoisted to the beginning of the scope, and the assignment x = 6; is fine because x has been declared somewhere in that scope.

Strict mode does not change any of this. It would throw an error if you omitted the var x; declaration altogether; without strict mode, the variable's scope would implicitly be the global scope.

In ES2015 (a.k.a. ES6), hoisting is avoided by using the let keyword instead of var. (The other difference is that variables declared with let are local to the surrounding block, not the entire function.)

There are some weird things javascript allows that, as someone learning the language, you must learn to combat with good coding practices (simicolons are another good example). In the case of hoisting, it is generally good practice to declare your variables at the top of the scope where they would be hoisted to anyway. As already mentioned, strict mode is not a silver bullet and will not enforce this for you.

发布评论

评论列表(0)

  1. 暂无评论