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/