[webkit-dev] Timer callback and 'this' object

Dmitry Titov dimich at chromium.org
Tue Dec 2 13:52:33 PST 2008


Thanks Sam,
I've sent a patch with a test.

Dmitry

On Mon, Dec 1, 2008 at 7:23 PM, Sam Weinig <sam.weinig at gmail.com> wrote:

> Hi Dmitry,
> The first example using a closure executes in the parent scope because
> functions execute in lexical scope regardless of where they are called from,
> capturing variables as required.  The string version is simply executed as a
> program by the caller and has no knowledge of the scope in which it was
> declared.  This is a bit strange, but follows the conventions of the
> language.  As for the test, tests are always welcomed, especially for odd
> edges like this.
>
> -Sam
>
> On Mon, Dec 1, 2008 at 8:08 PM, Dmitry Titov <dimich at chromium.org> wrote:
>
>> Hi webkit-dev,
>>
>> I'm looking at JS timeout code and found a callback behavior that seems
>> strange. Not sure if it is intentional.
>> If timeout is set on a 'window' object from another frame and timeout
>> callback is specified as a JS string, it is executed "inside the frame"...
>> So the context of execution is different depending how the callback is
>> specified.
>>
>> Here is html (also attached) that loads an empty IFRAME and sets 2
>> timeouts on that iframe's window - as a JS closure and as JS string. In
>> former case, the callback runs in the main page (expected, since the JS
>> closure 'captures' the context in which it is defined), in the latter-
>> inside iframe (a bit weird?). So the text goes in both "body" elements.
>>
>> Is there a reason why JS string is evaluated in the inner context of the
>> iframe? Would a test verifying this behavior be a good thing? Curiously, IE
>> and FF behave the same way.
>>
>> <html>
>> <script>
>> function test() {
>>   // Get the iframe's window.
>>   var iframeWindow = window.frames["testIframe"];
>>
>>   // setTimeout with a closure as callback.
>>   iframeWindow.setTimeout(
>>     function() {
>>       if (!document.body)
>>         document.write('<body></body>');
>>       document.body.appendChild(document.createTextNode('Timer 1
>> fired.'));}, 1);
>>
>>   // setTimeout with a JS string containing similar code.
>>   iframeWindow.setTimeout(
>>      "if (!document.body)" +
>>      "  document.write('<body></body>');" +
>>      "document.body.appendChild(document.createTextNode('Timer 2
>> fired.'));", 1);
>> }
>> </script>
>> <body onLoad="test();">
>> <iframe src="about:blank" id="testIframe"></iframe>
>> </body>
>> </html>
>>
>>
>> Thanks,
>> Dmitry
>>
>> _______________________________________________
>> webkit-dev mailing list
>> webkit-dev at lists.webkit.org
>> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20081202/49dd2bea/attachment.html>


More information about the webkit-dev mailing list