librelist archives

« back to archive

function creation in FF12

function creation in FF12

From:
York Wong
Date:
2012-05-16 @ 02:10
Anyone noticed in FF12, the creation of function is done by textual order
instead of before entering the execution context, so this is not working in
FF12 anymore:

test();
function test(){
alert('x');
}



-- 
Yours sincerely,
York

Re: [sydjs] function creation in FF12

From:
Mark Byers @ ibCom
Date:
2012-05-16 @ 02:56
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript

Mark Byers
mydigitalstructure.com
@mydsondemand



On 16/05/2012, at 12:10 PM, York Wong wrote:

> Anyone noticed in FF12, the creation of function is done by textual 
order instead of before entering the execution context, so this is not 
working in FF12 anymore:
> 
> test();
> function test(){
> alert('x');
> }
> 
> 
> 
> -- 
> Yours sincerely,
> York

Re: [sydjs] function creation in FF12

From:
Jared Wyles
Date:
2012-05-16 @ 03:12
That article has a few misnomers. See
http://es5.github.com/#primitive_valueall engines autobox so they
appear to be objects.

His ideas are on track but just presented in a strange way. He sort of
waves away the issue of hoisting and calls it caching?  i would recommend
reading
http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting for
the concepts. Then moving onto

http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/
http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/




On Wed, May 16, 2012 at 12:56 PM, Mark Byers @ ibCom
<mark.byers@ibcom.biz>wrote:

>
> 
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript
>
> Mark Byers
> mydigitalstructure.com
> @mydsondemand
>
>
>
> On 16/05/2012, at 12:10 PM, York Wong wrote:
>
> > Anyone noticed in FF12, the creation of function is done by textual
> order instead of before entering the execution context, so this is not
> working in FF12 anymore:
> >
> > test();
> > function test(){
> > alert('x');
> > }
> >
> >
> >
> > --
> > Yours sincerely,
> > York
>
>

Re: [sydjs] function creation in FF12

From:
Mark Byers @ ibCom
Date:
2012-05-16 @ 03:19
Yep - he is just the first one that comes up in Google...:-)

Cool.

The main thing is that it is not really a FF12 thing.

Mark Byers
mydigitalstructure.com
@mydsondemand



On 16/05/2012, at 1:12 PM, Jared Wyles wrote:

> That article has a few misnomers. See 
http://es5.github.com/#primitive_value all engines autobox so they appear 
to be objects. 
> 
> His ideas are on track but just presented in a strange way. He sort of 
waves away the issue of hoisting and calls it caching?  i would recommend 
reading 
> http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting for
the concepts. Then moving onto 
> 
> http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/
> http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/
> 
> 
> 
> 
> On Wed, May 16, 2012 at 12:56 PM, Mark Byers @ ibCom 
<mark.byers@ibcom.biz> wrote:
> 
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript
> 
> Mark Byers
> mydigitalstructure.com
> @mydsondemand
> 
> 
> 
> On 16/05/2012, at 12:10 PM, York Wong wrote:
> 
> > Anyone noticed in FF12, the creation of function is done by textual 
order instead of before entering the execution context, so this is not 
working in FF12 anymore:
> >
> > test();
> > function test(){
> > alert('x');
> > }
> >
> >
> >
> > --
> > Yours sincerely,
> > York
> 
> 

Re: [sydjs] function creation in FF12

From:
Jared Wyles
Date:
2012-05-16 @ 03:16
Oh also if you want some in-depth looks into how engines implement types at
run time.

http://evilpie.github.com/sayrer-fatval-backup/cache.aspx.htm

scroll down to "new javascript value representation"


http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSValue.h#L250for
webkit

and

http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/objects.h#112for
v8 :D

SMI = small integer btw.

On Wed, May 16, 2012 at 1:12 PM, Jared Wyles <jared.wyles@gmail.com> wrote:

> That article has a few misnomers. See
> http://es5.github.com/#primitive_value all engines autobox so they appear
> to be objects.
>
> His ideas are on track but just presented in a strange way. He sort of
> waves away the issue of hoisting and calls it caching?  i would recommend
> reading
> http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting for
> the concepts. Then moving onto
>
> http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/
> http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/
>
>
>
>
> On Wed, May 16, 2012 at 12:56 PM, Mark Byers @ ibCom <mark.byers@ibcom.biz
> > wrote:
>
>>
>> 
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript
>>
>> Mark Byers
>> mydigitalstructure.com
>> @mydsondemand
>>
>>
>>
>> On 16/05/2012, at 12:10 PM, York Wong wrote:
>>
>> > Anyone noticed in FF12, the creation of function is done by textual
>> order instead of before entering the execution context, so this is not
>> working in FF12 anymore:
>> >
>> > test();
>> > function test(){
>> > alert('x');
>> > }
>> >
>> >
>> >
>> > --
>> > Yours sincerely,
>> > York
>>
>>
>

Re: [sydjs] function creation in FF12

From:
Mark Byers @ ibCom
Date:
2012-05-16 @ 03:33
Thanks for sharing links Jared!  I've added to our js resource page 
http://mydigitalstructure.com/gettingstarted_js

Mark Byers
mydigitalstructure.com
@mydsondemand



On 16/05/2012, at 1:16 PM, Jared Wyles wrote:

> Oh also if you want some in-depth looks into how engines implement types
at run time.
> 
> http://evilpie.github.com/sayrer-fatval-backup/cache.aspx.htm
> 
> scroll down to "new javascript value representation"
> 
> 
http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSValue.h#L250
for webkit 
> 
> and 
http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/objects.h#112
for v8 :D
> 
> SMI = small integer btw. 
> 
> On Wed, May 16, 2012 at 1:12 PM, Jared Wyles <jared.wyles@gmail.com> wrote:
> That article has a few misnomers. See 
http://es5.github.com/#primitive_value all engines autobox so they appear 
to be objects. 
> 
> His ideas are on track but just presented in a strange way. He sort of 
waves away the issue of hoisting and calls it caching?  i would recommend 
reading 
> http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting for
the concepts. Then moving onto 
> 
> http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/
> http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/
> 
> 
> 
> 
> On Wed, May 16, 2012 at 12:56 PM, Mark Byers @ ibCom 
<mark.byers@ibcom.biz> wrote:
> 
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript
> 
> Mark Byers
> mydigitalstructure.com
> @mydsondemand
> 
> 
> 
> On 16/05/2012, at 12:10 PM, York Wong wrote:
> 
> > Anyone noticed in FF12, the creation of function is done by textual 
order instead of before entering the execution context, so this is not 
working in FF12 anymore:
> >
> > test();
> > function test(){
> > alert('x');
> > }
> >
> >
> >
> > --
> > Yours sincerely,
> > York
> 
> 
> 

Re: [sydjs] function creation in FF12

From:
Jared Wyles
Date:
2012-05-16 @ 03:53
Ok.
I have read the spec.

I am still a little bit confused, but these are my initial feelings.

The relevant part of the spec is

If the variable statement occurs inside a FunctionDeclaration, the
variables are defined with function-local scope in that function, as
described in section 10.1.3. Otherwise, they are defined with global
scope (that is, they are created as members of the global object, as
described in section 10.1.3) using property attributes { DontDelete }.
Variables are created when the execution scope is entered. A Block
does not define a new execution scope. Only Program and
FunctionDeclaration produce a new scope. Variables are initialised to
undefined when created. A variable with an Initialiser is assigned the
value of its AssignmentExpression when the VariableStatement is
executed, not when the variable is created.

This basically describes hoisting.

VariableEnvironment Identifies the Lexical Environment whose
environment record holds
bindings created by  VariableStatements and  FunctionDeclarations within
this execution context.

This is defining how a variableEnviroment is created, notice that it
only lists functionDeclarations can modify the variableEnviroment?

The tricky bit comes down to the way a functionExpression and a
functionDeclaration is defined in the spec.

For those playing a long at home, an expression is a building block of
a language. It is basically any thign that returns a value as part of
its execution. (Most things are expressions in javascript)/

FunctionDeclaration :
function Identifier ( FormalParameterList opt ) { FunctionBody }

FunctionExpression :
function Identifier opt ( FormalParameterList opt ) { FunctionBody }

Notice the syntax is the same except for the optional identifier. I
think this allows for some liberal interpretation here.

as function hello() { } is valid as both a declaration and an
expression. So it really comes down to context.

var a = function hello() {
}; Would be treated as an expression.

while
function hello() {
} is treated as an declaration.

They could be either given the spec. I think what firefox is doing is
simply extending this and treating anything inside a loop as an
expression, which as we all know does not get hoisted.

This is my interpretation, i could be very wrong. It makes sense in my head :)

Hopefully this can at least be a starting point for discussion of why :)

Thanks,
Jared.


On Wed, May 16, 2012 at 1:33 PM, Mark Byers @ ibCom
<mark.byers@ibcom.biz> wrote:
>
> Thanks for sharing links Jared!  I've added to our js resource 
page http://mydigitalstructure.com/gettingstarted_js
>
> Mark Byers
> mydigitalstructure.com
> @mydsondemand
>
>
>
> On 16/05/2012, at 1:16 PM, Jared Wyles wrote:
>
> Oh also if you want some in-depth looks into how engines implement types
at run time.
>
> http://evilpie.github.com/sayrer-fatval-backup/cache.aspx.htm
>
> scroll down to "new javascript value representation"
>
> 
http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSValue.h#L250
for webkit
>
> 
and http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/objects.h#112
for v8 :D
>
> SMI = small integer btw.
>
> On Wed, May 16, 2012 at 1:12 PM, Jared Wyles <jared.wyles@gmail.com> wrote:
>>
>> That article has a few misnomers. 
See http://es5.github.com/#primitive_value all engines autobox so they 
appear to be objects.
>>
>> His ideas are on track but just presented in a strange way. He sort of 
waves away the issue of hoisting and calls it caching?  i would recommend 
reading
>> http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting 
for the concepts. Then moving onto
>>
>> http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/
>> http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/
>>
>>
>>
>>
>> On Wed, May 16, 2012 at 12:56 PM, Mark Byers @ ibCom 
<mark.byers@ibcom.biz> wrote:
>>>
>>> 
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript
>>>
>>> Mark Byers
>>> mydigitalstructure.com
>>> @mydsondemand
>>>
>>>
>>>
>>> On 16/05/2012, at 12:10 PM, York Wong wrote:
>>>
>>> > Anyone noticed in FF12, the creation of function is done by textual 
order instead of before entering the execution context, so this is not 
working in FF12 anymore:
>>> >
>>> > test();
>>> > function test(){
>>> > alert('x');
>>> > }
>>> >
>>> >
>>> >
>>> > --
>>> > Yours sincerely,
>>> > York
>>>
>>
>
>

Re: [sydjs] function creation in FF12

From:
Jared Wyles
Date:
2012-05-16 @ 05:16
I guess this muddiness in the spec has been picked up in ecmascript 5,
as there is now a note warning about this specific case.


NOTE Several widely used implementations of ECMAScript are known to
support the use of FunctionDeclaration as a
Statement. However there are significant and irreconcilable variations
among the implementations in the semantics applied
to such FunctionDeclarations. Because of these irreconcilable
differences, the use of a FunctionDeclaration as a Statement
results in code that is not reliably portable among implementations.
It is recommended that ECMAScript implementations
either disallow this usage of FunctionDeclaration or issue a warning
when such a usage is encountered. Future editions of
ECMAScript may define alternative portable means for declaring
functions in a Statement context.

Jared.


On Wed, May 16, 2012 at 1:53 PM, Jared Wyles <jared.wyles@gmail.com> wrote:
> Ok.
> I have read the spec.
>
> I am still a little bit confused, but these are my initial feelings.
>
> The relevant part of the spec is
>
> If the variable statement occurs inside a FunctionDeclaration, the
> variables are defined with function-local scope in that function, as
> described in section 10.1.3. Otherwise, they are defined with global
> scope (that is, they are created as members of the global object, as
> described in section 10.1.3) using property attributes { DontDelete }.
> Variables are created when the execution scope is entered. A Block
> does not define a new execution scope. Only Program and
> FunctionDeclaration produce a new scope. Variables are initialised to
> undefined when created. A variable with an Initialiser is assigned the
> value of its AssignmentExpression when the VariableStatement is
> executed, not when the variable is created.
>
> This basically describes hoisting.
>
> VariableEnvironment Identifies the Lexical Environment whose
> environment record holds
> bindings created by  VariableStatements and  FunctionDeclarations within
> this execution context.
>
> This is defining how a variableEnviroment is created, notice that it
> only lists functionDeclarations can modify the variableEnviroment?
>
> The tricky bit comes down to the way a functionExpression and a
> functionDeclaration is defined in the spec.
>
> For those playing a long at home, an expression is a building block of
> a language. It is basically any thign that returns a value as part of
> its execution. (Most things are expressions in javascript)/
>
> FunctionDeclaration :
> function Identifier ( FormalParameterList opt ) { FunctionBody }
>
> FunctionExpression :
> function Identifier opt ( FormalParameterList opt ) { FunctionBody }
>
> Notice the syntax is the same except for the optional identifier. I
> think this allows for some liberal interpretation here.
>
> as function hello() { } is valid as both a declaration and an
> expression. So it really comes down to context.
>
> var a = function hello() {
> }; Would be treated as an expression.
>
> while
> function hello() {
> } is treated as an declaration.
>
> They could be either given the spec. I think what firefox is doing is
> simply extending this and treating anything inside a loop as an
> expression, which as we all know does not get hoisted.
>
> This is my interpretation, i could be very wrong. It makes sense in my head :)
>
> Hopefully this can at least be a starting point for discussion of why :)
>
> Thanks,
> Jared.
>
>
> On Wed, May 16, 2012 at 1:33 PM, Mark Byers @ ibCom
> <mark.byers@ibcom.biz> wrote:
>>
>> Thanks for sharing links Jared!  I've added to our js resource 
page http://mydigitalstructure.com/gettingstarted_js
>>
>> Mark Byers
>> mydigitalstructure.com
>> @mydsondemand
>>
>>
>>
>> On 16/05/2012, at 1:16 PM, Jared Wyles wrote:
>>
>> Oh also if you want some in-depth looks into how 
engines implement types at run time.
>>
>> http://evilpie.github.com/sayrer-fatval-backup/cache.aspx.htm
>>
>> scroll down to "new javascript value representation"
>>
>> 
http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSValue.h#L250
for webkit
>>
>> 
and http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/objects.h#112
for v8 :D
>>
>> SMI = small integer btw.
>>
>> On Wed, May 16, 2012 at 1:12 PM, Jared Wyles <jared.wyles@gmail.com> wrote:
>>>
>>> That article has a few misnomers. 
See http://es5.github.com/#primitive_value all engines autobox so they 
appear to be objects.
>>>
>>> His ideas are on track but just presented in a strange way. He sort of
waves away the issue of hoisting and calls it caching?  i would recommend 
reading
>>> http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting 
for the concepts. Then moving onto
>>>
>>> http://dmitrysoshnikov.com/ecmascript/chapter-4-scope-chain/
>>> http://dmitrysoshnikov.com/notes/note-4-two-words-about-hoisting/
>>>
>>>
>>>
>>>
>>> On Wed, May 16, 2012 at 12:56 PM, Mark Byers @ ibCom 
<mark.byers@ibcom.biz> wrote:
>>>>
>>>> 
http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript
>>>>
>>>> Mark Byers
>>>> mydigitalstructure.com
>>>> @mydsondemand
>>>>
>>>>
>>>>
>>>> On 16/05/2012, at 12:10 PM, York Wong wrote:
>>>>
>>>> > Anyone noticed in FF12, the creation of function is done by textual
order instead of before entering the execution context, so this is not 
working in FF12 anymore:
>>>> >
>>>> > test();
>>>> > function test(){
>>>> > alert('x');
>>>> > }
>>>> >
>>>> >
>>>> >
>>>> > --
>>>> > Yours sincerely,
>>>> > York
>>>>
>>>
>>
>>

Re: [sydjs] function creation in FF12

From:
Jared Wyles
Date:
2012-05-16 @ 02:15
just tried it in FF12 works as expected for me.

On Wed, May 16, 2012 at 12:10 PM, York Wong <wongyok@gmail.com> wrote:

> Anyone noticed in FF12, the creation of function is done by textual order
> instead of before entering the execution context, so this is not working in
> FF12 anymore:
>
> test();
> function test(){
> alert('x');
> }
>
>
>
> --
> Yours sincerely,
> York
>

Re: [sydjs] function creation in FF12

From:
John Bristowe
Date:
2012-05-16 @ 02:35
Please define "this is not working in FF12 anymore". What does this mean? 
Are you not seeing an alert raised? Or, are you seeing an error generated?

From: York Wong <wongyok@gmail.com<mailto:wongyok@gmail.com>>
Reply-To: "sydjs@librelist.com<mailto:sydjs@librelist.com>" 
<sydjs@librelist.com<mailto:sydjs@librelist.com>>
Date: Wednesday, 16 May 2012 12:10 PM
To: "sydjs@librelist.com<mailto:sydjs@librelist.com>" 
<sydjs@librelist.com<mailto:sydjs@librelist.com>>
Subject: [sydjs] function creation in FF12

Anyone noticed in FF12, the creation of function is done by textual order 
instead of before entering the execution context, so this is not working 
in FF12 anymore:

test();
function test(){
alert('x');
}



--
Yours sincerely,
York

Re: [sydjs] function creation in FF12

From:
Ben Joffe
Date:
2012-05-16 @ 02:50
I noticed this as well, but it only happens under certain
circumstances, eg. when inside an if statement:

<script>
if (true) {
  test(); // firefox throws 'test is not defined'
  function test(){
    alert('x');
  }
}
</script>

It's not good practice to place functions inside 'if' statements like
this, as the function will be created regardless of whether the branch
executes.

This has been happening for at least a few firefox versions

Cheers,
Ben

On 16 May 2012 12:35, John Bristowe <John.Bristowe@telerik.com> wrote:
> Please define "this is not working in FF12 anymore". What does this mean?
> Are you not seeing an alert raised? Or, are you seeing an error generated?
>
> From: York Wong <wongyok@gmail.com>
> Reply-To: "sydjs@librelist.com" <sydjs@librelist.com>
> Date: Wednesday, 16 May 2012 12:10 PM
> To: "sydjs@librelist.com" <sydjs@librelist.com>
> Subject: [sydjs] function creation in FF12
>
> Anyone noticed in FF12, the creation of function is done by textual order
> instead of before entering the execution context, so this is not working in
> FF12 anymore:
>
> test();
> function test(){
> alert('x');
> }
>
>
>
> --
> Yours sincerely,
> York

Re: [sydjs] function creation in FF12

From:
Mark Byers @ ibCom
Date:
2012-05-16 @ 02:51
This is the same in FF11.

If you cut out the test(); and run it - the function is then "registered" 
and then put the test(); back in before the function it will work.   Not 
of course a solution - but shows not just a FF12 thing.

Mark Byers
mydigitalstructure.com
@mydsondemand



On 16/05/2012, at 12:35 PM, John Bristowe wrote:

> Please define "this is not working in FF12 anymore". What does this 
mean? Are you not seeing an alert raised? Or, are you seeing an error 
generated?
> 
> From: York Wong <wongyok@gmail.com>
> Reply-To: "sydjs@librelist.com" <sydjs@librelist.com>
> Date: Wednesday, 16 May 2012 12:10 PM
> To: "sydjs@librelist.com" <sydjs@librelist.com>
> Subject: [sydjs] function creation in FF12
> 
> Anyone noticed in FF12, the creation of function is done by textual 
order instead of before entering the execution context, so this is not 
working in FF12 anymore:
> 
> test();
> function test(){
> alert('x');
> }
> 
> 
> 
> -- 
> Yours sincerely,
> York