How to assign new function within a function?

H

howa

The topic is not very good, so please see the example below...


function bar(fn) {

fn.hi = function() {
alert('hi');
}
};

bar(

function() {
this.hi();
}

);

What I want is I want the have an alert popup in the above code, is it
possible?


Thanks.
 
S

Stevo

howa said:
The topic is not very good, so please see the example below...
...snip..

What I want is I want the have an alert popup in the above code, is it
possible?

Don't know about anyone else but I'm certainly confused.
 
T

Thomas 'PointedEars' Lahn

howa said:
function bar(fn) {

fn.hi = function() {
alert('hi');
}

This statement should be delimited with `;' to distinguish the assignment of
the function expression from a function declaration.

This line and the lines before compose a statement that is a function
declaration; it does not need to be delimited with `;', it is delimited
with `}' already.

// works
function foo() { window.alert("bar"); }foo();
bar(

function() {
this.hi();
}

);

That would pass a Function object, created by the function expression, to
`bar' and add a hi() method to it. Since there is no reference to that
Function object, it is marked for garbage collection after the call is
completed.
What I want is I want the have an alert popup in the above code, is it
possible?

Yes.


// or: var o = {}; (JavaScript 1.3+, JScript 3+, ECMAScript 3+ [1])
var o = new Object();

bar(o);
o.hi();

If you want to add a property to a Function object instead, you have to pass
a named reference to that object in order to use it afterwards:

var f = new Function(...);

or

var f = function(...) {
...
};

and then

bar(f);
f.hi();

As you can see, as in functional programming languages functions are
first-class objects in ECMAScript implementations.


PointedEars
___________
[1] http://PointedEars.de/scripts/es-matrix/
 
H

howa

If you want to add a property to a Function object instead, you have to pass
a named reference to that object in order to use it afterwards:

so what are the differences between a named reference and without a
name?

e.g.

var f = new Function(...);
bar(f);

and ...

bar( function(...) };

I assume at the bar()'s internal, they should be the same, i.e.
reference to a function?


Thanks.

Howard
 
T

Thomas 'PointedEars' Lahn

howa said:
so what are the differences between a named reference and without a
name?

e.g.
(1)
var f = new Function(...);

or

var f = function(...) {
...
};
bar(f);

and ...
(2)
bar( function(...) };

Must be:

bar( function(...) { ... } );

(1) declares a property of the Variable Object of the execution context (in
global context: the Global Object) and assigns the Function object reference
to it. (2) does not.
I assume at the bar()'s internal, they should be the same, i.e.
reference to a function?

Yes, but even if we assume that the object was not automatically
garbage-collected, there would not be much use in an object that
you can refer to later, now would it? The identifier of `f' would allow that.

There is a possibility to prevent garbage collection and go without names at
first, though: return the passed object reference from bar(). But you would
probably want to assign it to a property somewhere later, and if it was only
for debugging.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
howa wrote:
[...]
I assume at the bar()'s internal, they should be the same, i.e.
reference to a function?

Yes, but even if we assume that the object was not automatically
garbage-collected, there would not be much use in an object that
you can refer to later, now would it? The identifier of `f' would allow that. ^^^
cannot

There is a possibility to prevent garbage collection and go without names at
first, though: [...]
 
H

howa

Yes, but even if we assume that the object was not automatically
garbage-collected, there would not be much use in an object that
you can refer to later, now would it? The identifier of `f' would allow that.

Great info with in depth JS knowledges, thanks!

Prototype.js was written by people who don't know javascript for people
who don't know javascript.

Well, why do you say so?



Thanks again!

Howard
 
T

Thomas 'PointedEars' Lahn

howa said:
Yes, but even if we assume that the object was not automatically
garbage-collected, there would not be much use in an object that you
[cannot] refer to later, now would it? The identifier of `f' would
allow that.

Great info with in depth JS knowledges, thanks!

You are welcome.
Well, why do you say so?

I do not; you are quoting my signature which in turn quotes Richard
Cornford. However, I use that signature because I agree with it as
Prototype.js certainly looks like that. See also previous discussions.


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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top