function katana(){
this.isSharp = true;
}
katana();
assert( isSharp === true, "A global object now exists with that name and value." );
var shuriken = {
toss: function(){
this.isSharp = true;
}
};
shuriken.toss();
assert( shuriken.isSharp === true, "When it's an object property, the value is set within the object." );
I am actually not getting what is this code trying to tell?. Can anyone explain me what is Context in JavaScript and what exactly does context represent here in the above code?
Well, the reason
this === windowin the first example andthis === shurikenin the second example all has to do with where those functions are created. Notice that when you defineshuirken.tossoutside of the object,thispoints to the window object. And when you callkatanawithnew,thispoints to the newly created object.