Here is a snippet of code
function test() { this.value = "foo"; } $(document).ready(function () { test(); alert(this.value); //--> return undefined alert(window.value); //--> return 'foo' });
Can someone explain these results to me?
Hi
Salvatore
In your function test(), thisrefers toDOMWindow
test()
this
DOMWindow
The $(document).ready()function thismeant document.
$(document).ready()
document
So, since in test()you set the window value, therefore window.value ==> 'foo', butdocument.value ==> undefined
window.value ==> 'foo'
document.value ==> undefined
Read this article on a feature area that might help.
this - This is a difficult keyword to get your head around.
I advise you to read this, this may help a little more http://www.quirksmode.org/js/this.html
: , this.value . jQuery document.ready(..).
this.value
document.ready(..)
Javascript this - , .
, , , : http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this p >
function test() { this.value = "foo"; // this is window } $(document).ready(function () { test(); alert(this.value); //--> this is window.document alert(window.value); //--> return 'foo' });
jquery, :
$(document).ready(...)
, onReady . ( ).
onReady,
alert(this.value); //--> return undefined
return undefined, .
test();
, , javascript , .
, , , , , ( javascript ), , , ,
alert(window.value); //--> return 'foo'
?
( ) window, value window.
,