Experts, what do you think of a.b = new function MyFunction() { ... }?

B

blackholebutterfly

Is the following a useful form to use:

a.b = new function MyFunction() {

execute();
function execute() { ... }

function methodTwo() { ... }
}

It seems to me not a very useful way to arrange things. Is it
necessary to have a function name after 'new function'?
It also seems that the second function (methodTwo()) cannot be called
in any way (or maybe I have that wrong).

What are the problems with doing things like this and what is the best
alternative?

Thanks,
blackholebutterfly
 
R

Richard Cornford

(e-mail address removed) wrote:

It probably is not that good an idea to put "Experts" in your subject.
Partly because this is Usenet, where trying to dictate the responses you get
is futile. And partly because the process of transitioning form believing
you are an 'expert' and actually being an expert tends to be humbling,
leaving the latter inclined to question the significance of the label.
Is the following a useful form to use:

a.b = new function MyFunction() {

execute();
function execute() { ... }

function methodTwo() { ... }
}

The form of your question (from the posted code) appears to be about a very
specific structure, and a structure that is pretty perverse. The code looks
like it may have been stripped down from a real example, but the material
stripped out probably contained the only possible justification for this
structure.

First off; the - new - operator applied to a FunctionExpression results is
the creation of a new object, that then becomes the value of the - this -
keyword during the execution of the FunctionExpression's body (excluding any
execution of inner functions). This code does not include any uses off the -
this - keyword (and there is no opportunity to modify the
FunctionExpression's prototype), so the object created is never
modified/augmented by the fact that the FunctionExpression is used as a
constructor. The object still gets assigned to the 'b' property of the
object referred to by - a -, so the overall effect is the same as:-

a.b = new Object();

Then there are side effects of that process. Those side effects include the
creation of two function objects (- execute - and - methodTwo -) and the
execution of one of them. No references to these two inner functions ever
get assigned to anything outside of the - MyFunction - FunctionExpression's
execution context so no closures are formed here.

That leaves the main side-effect of assigning an ordinary (unmodified)
object to - a.b - as being the execution of the - execute - function. So the
whole thing could be written as:-

(function(){
...
})():
a.b = {};

- where the body of the anonymous function above is the equivalent of the
body of the - execute - function. The outcome would be the same but the code
that achieves it is shorter faster and clearer. Clearer not least because
the side-effect of the assignment in your code has become an distinct
process independent of the assignment.

Commenting on what the pre stripped-down code may have been about is not
that practical without seeing it. Though there is a common (bad) practice
where the - new - operator is applied to FunctionExpressions in place of
directly calling them (as I have done in my example). Those bad practice
examples can be identified by observing the actions performed on the object
created. Where that object has no significance in what the code does then
the - new - operator was an inappropriate means of achieving the execution
of the FunctionExpression.
It seems to me not a very useful way to arrange things.

As it stands, not very useful.
Is it necessary to have a function name after
'new function'?

No, and it is actually a very bad idea because it provokes an IE bug where
two function objects get created as a result of the code. One during
variable instantiation for the containing execution contexts as a named
property of the variable object, and one during the evaluation of the
FunctionExpression. Apart from not being very efficient, this has the side
effect of messing up the scoping if you attempt to do the only thing that
justifies the ability to use Identifiers with FunctionExpressions; to allow
code inside the function to refer to itself (the function object) by name.
If the code inside the FunctionExpression attempt to refer to itself by name
there are circumstance where it will actually be referring to the function
object created during variable instantiation, a different function object.
(The difference might be insignificant, or a really hard to spot bug).

This leads to the practical conclusion that if FunctionExpressions are to be
used in cross browser code at all they should be anonymous.
It also seems that the second function (methodTwo()) cannot
be called in any way (or maybe I have that wrong).

As your code has it the methodTwo function cannot be called and the creation
of the corresponding function object represents no more than a pointless
runtime overhead. But this is so self-evident that it is hard to believe
that the code you cut from the example above did not include something that
would justify it.
What are the problems with doing things like this

The consequences of writing code that makes no apparent sense, is
convoluted, and is also inefficient don't really need to be stated.
and what is the best alternative?

Find out what it is that is wanted from the code and then program the code
that achieves it (preferably reasonably clearly, efficiently and directly).
Much the same as always.

Richard.
 
T

Thomas 'PointedEars' Lahn

Is the following a useful form to use:

a.b = new function MyFunction() {
[...]
}

That would depend on how useful you deem a plain syntax error.


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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top