Tuesday 29 January 2013

JavaScript - Predict the Output - Question 1

"use strict";
function strict() {
  "use strict"; // top of the function
  return {
    withStrict: function(){
      return this; // undefined
    }(),
    withoutStrict: Function("return this")()
  };
}
var really = strict();
really.withStrict; 
You must specify the reason too.

3 comments:

  1. Undefined !!!!! Without strict mode, the 'this' here refers to the global window object... But when you use strict mode , the keyword this no more refers to the global window object .. hence UNDEFINED

    Yuppppp:::)))

    ReplyDelete
  2. Wouldn't it just return the withStrict function? As in:
    function(){ return this; }

    The final line doesn't actually call the function, so it would return the function itself.

    ReplyDelete