Monday 4 February 2013

JavaScript - Predict the Output - Question 2


Object.defineProperty(this, "MAX_SIZE", {
  value: 100
});

alert("OLD VALUE "+ MAX_SIZE); 

MAX_SIZE = 200; 

alert("NEW VALUE "+MAX_SIZE);

What would be the output & why?

4 comments:

  1. 100 both the times because of hoisting?

    ReplyDelete
  2. step1 : Object.defineProperty(this, "MAX_SIZE", {
    value: 100
    }); // setting a new property to window object(this refers to window object). and its not writable through assignment. you can change this value.


    step2 : MAX_SIZE = 200;

    with out var keyword all the variables are belongs to window object , here we are trying to change the property of the window object which we created in first step and its not possible.

    so both times it will be same.

    ReplyDelete
    Replies
    1. sorry typo "its not writable through assignment. you can not change this value.)

      Delete