nested function doesn't work

S

Stone Zhong

Hi,

I am learning JavaScript, so please pardon me if my question is naive.

I bought a book "JavaScript The Definitive Guide, 3rd Edition" by
David Flanagan, it is a used book and only cost me $1. :), on page
199-200, there is a sample program like below, instead of getting the
outout the book suggested, Firefox(3.6.3) shows an error: "f.g is not
a function".

I undertand the book is about JavaScript 1.2, is there anything wrong
with the sample?

Thanks,
Stone

var x = "global";
function f() {
var x = "local";
function g() {
alert(x);
}
}
// Display "undefined", f() as not been invoked yet, so
// while the local variable x exists in its call object, it has not
// yet had a value assigned to it.
f.g();
// Calling f() initializes the variable
f();
// Now when we invoke the nested function we obtain the value of
// f's local variable
f.g(); // displays "local"
 
D

David Mark

Stone said:
Hi,

I am learning JavaScript, so please pardon me if my question is naive.

Naive questions are certainly welcome here.
I bought a book "JavaScript The Definitive Guide, 3rd Edition" by
David Flanagan, it is a used book and only cost me $1. :),

I think you'll find it is worth every penny. :)
on page
199-200, there is a sample program like below, instead of getting the
outout the book suggested, Firefox(3.6.3) shows an error: "f.g is not
a function".

Oops, perhaps I have overstated its value.
I undertand the book is about JavaScript 1.2, is there anything wrong
with the sample?


[...]


var x = "global";
function f() {
var x = "local";
function g() {
alert(x);
}
}
// Display "undefined", f() as not been invoked yet, so
// while the local variable x exists in its call object, it has not
// yet had a value assigned to it.
f.g();

There's something wrong with that for sure. There is no "g" property of
the Function object referenced by f. Not even in JS 1.2.

Was that really in the book?
// Calling f() initializes the variable
f();
// Now when we invoke the nested function we obtain the value of
// f's local variable
f.g(); // displays "local"

Also nonsense (for the same reason). Flanagan's technical editor must
have been on holiday.
 
S

Stone Zhong

Stone said:
I am learning JavaScript, so please pardon me if my question is naive.

Naive questions are certainly welcome here.


I bought a book "JavaScript The Definitive Guide, 3rd Edition" by
David Flanagan, it is a used book and only cost me $1. :),

I think you'll find it is worth every penny.  :)
on page
199-200, there is a sample program like below, instead of getting the
outout the book suggested, Firefox(3.6.3) shows an error: "f.g is not
a function".

Oops, perhaps I have overstated its value.


I undertand the book is about JavaScript 1.2, is there anything wrong
with the sample?
[...]



var x = "global";
function f() {
    var x = "local";
    function g() {
        alert(x);
    }
}
// Display "undefined", f() as not been invoked yet, so
// while the local variable x exists in its call object, it has not
// yet had a value assigned to it.
f.g();

There's something wrong with that for sure.  There is no "g" property of
the Function object referenced by f.  Not even in JS 1.2.

Was that really in the book?
// Calling f() initializes the variable
f();
// Now when we invoke the nested function we obtain the value of
// f's local variable
f.g(); // displays "local"

Also nonsense (for the same reason).  Flanagan's technical editor must
have been on holiday.

Thank you for the reply Mr. Mark. I have double checked there is no
typo. Anyway, 3rd edition was published in 1998, I am thinking about
getting the latest 6th edition of this book.
 
D

David Mark

Stone said:
Stone said:
Hi,
I am learning JavaScript, so please pardon me if my question is naive.
Naive questions are certainly welcome here.


I bought a book "JavaScript The Definitive Guide, 3rd Edition" by
David Flanagan, it is a used book and only cost me $1. :),
I think you'll find it is worth every penny. :)
on page
199-200, there is a sample program like below, instead of getting the
outout the book suggested, Firefox(3.6.3) shows an error: "f.g is not
a function".
Oops, perhaps I have overstated its value.


I undertand the book is about JavaScript 1.2, is there anything wrong
with the sample? [...]



var x = "global";
function f() {
var x = "local";
function g() {
alert(x);
}
}
// Display "undefined", f() as not been invoked yet, so
// while the local variable x exists in its call object, it has not
// yet had a value assigned to it.
f.g();
There's something wrong with that for sure. There is no "g" property of
the Function object referenced by f. Not even in JS 1.2.

Was that really in the book?
// Calling f() initializes the variable
f();
// Now when we invoke the nested function we obtain the value of
// f's local variable
f.g(); // displays "local"
Also nonsense (for the same reason). Flanagan's technical editor must
have been on holiday.

Thank you for the reply Mr. Mark. I have double checked there is no
typo. Anyway, 3rd edition was published in 1998, I am thinking about
getting the latest 6th edition of this book.

Save your money.
 
T

Thomas 'PointedEars' Lahn

David said:
There's something wrong with that for sure. There is no "g" property of
the Function object referenced by f. Not even in JS 1.2.

JavaScript before version 1.2 did not support nested function declarations.
The call is supported, though, in JavaScript 1.2 to 1.5, i.e. from Netscape
4.0 to Gecko 1.7.13 (which includes Mozilla up to version 1.7.13 and Firefox
up to version 1.0.8).

<http://PointedEars.de/es-matrix#javascript>

However, it is not possible for the function so called to work as a closure,
i.e. `x' has the value `undefined' in there. The following would yield 42
(but not in recent browsers, of course):

function f()
{
function g()
{
return 42;
}
}

/* 42 */
window.alert(f.g());

This incident yet again emphasizes the importance of telling about the
environment in which a script is supposed to run.


PointedEars
 
D

David Mark

Thomas said:
JavaScript before version 1.2 did not support nested function declarations.
The call is supported, though, in JavaScript 1.2 to 1.5, i.e. from Netscape
4.0 to Gecko 1.7.13 (which includes Mozilla up to version 1.7.13 and Firefox
up to version 1.0.8).

<http://PointedEars.de/es-matrix#javascript>

However, it is not possible for the function so called to work as a closure,
i.e. `x' has the value `undefined' in there. The following would yield 42
(but not in recent browsers, of course):

function f()
{
function g()
{
return 42;
}
}

/* 42 */
window.alert(f.g());

That still makes no sense to me (clearly it is a structure I managed to
avoid). Nested or not, how would g become a method of f?
 
T

Thomas 'PointedEars' Lahn

David said:
That still makes no sense to me (clearly it is a structure I managed to
avoid). Nested or not, how would g become a method of f?

(Your question forces me to full-quote this.)

Possibilities: Early variable instantiation of a function declaration within
a function declaration

A) creates a property, having the inner function declaration's identifier
as name, on the Function instance created by variable instantiation of
the outer function declaration.

B) creates the Variable Object of the Function instance created by the
outer function declaration early, and puts it in the prototype chain
of that Function instance. But ISTM that can be excluded since the
objects referred to by `f.….__proto__' do not have a `g' property.

What I can say for sure is that it provably works in the named
implementations (I shall test other implementations, for the ES Matrix), and
was later dropped for the obvious inconsistency with the scope mechanism of
ECMAScript Edition 3 (I would not be surprised if it was still supported in
later versions with language="JavaScript1.2"; perhaps I should extend the
Matrix to test that as well). A leftover from less civi^Wstandardized
times, if you will.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top