Re: Finding heigth and widthof image

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Trevor L. wrote:
<snip>
I have found that new fname(){.....} allows one to define the
function *and also* executes it.

No it does not. That will produce a syntax error.

But why does it work (without syntax error) ??

It does not work, it generates a syntax error. Replacing the dots with
an empty statement to produce:-

new fname(){;}

- results in an "Expected ';'" error. It is expecting the ';' after the
closing parenthesis following the arguments list for the constructor.

Here is an exact copy of the code (added by cut and paste) which
is inside another function
image = "images/" + Picturesx[collection][ss_ImgNum] + ".jpg"
new function testit()
^^^^^^^^^
This is not the form of expression shown above, it is using a function
expression, with optional Identifier, as a constructor.

<snip>
} // ------ end testit() ------

I assume the alternative is
function fnname(){.....}
fnname()

Is this more efficient ?

How "efficient" do you need your syntax errors?

Are you saying that the following will return a syntax error?
function fnname(){.....}
fnname()

No, you asked me about alternatives to code that generates a syntax
error.

I have not found it so. The first line defines the function named
fnname and the second executes it. Or at least the results seem
to confirm that this is what is happening.

That may be what that code does, it is not what - new fname(){ ... } -
does.

Or what is the more direct alternative?

The original form - new function(){ ... }; - is object construction,
where the execution of the constructor body is part of the process. If
you want to construct an object then object construction is indicated.
If the intention is only to execute the body of an anonymous function
then just calling that function is the more direct, obvious, simple
and efficient alternative:-

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

I understand a liitle of this. I think you are saying (confirming) that the
use of the new ... construct *does* both define and execute the
function (or object or whatever).

new constructor; - (where 'constructor' is a reference to a function
object) constructs a new object, executing the body of the constructor
(with the - this - keyword referring to that new object) as part of the
process.

Is an "anonymous function" a function that one can define a function
ithout naming it?

Yes, a function created with a function expression (though function
expressions do not necessarily result in anonymous functions as they
may have an optional Identifier (so that they may refer to themselves
by name)).

If so, then I am not sure that this is what I want because the
unction loadit() calls testit(), therefore testit() can't have no name.

It can, but that is a different story. The important part of this is
that the operand of the new operator is a function expression (not
anonymous in your case).

The intention is to define and execute a function
Does this do that (when the code for the function replaces ...) ?:
(function(){ ... })();

If you need the name put the optional Identifier in (though most
ECMAScript implementations are buggy in the area of optional
Identifiers with function expressions (they may make them visible form
outside the expression, in different ways.).

(function testit(){ ... })();

Why are there brackets around the whole function defintion ?

An expression statement may not commence with the keyword - function -
else it could be confused with a function declaration.

Why are there two sets of brackets (both after the word "function" and
after the function defintion) ?

The first prevent the - function - keyword being interpreted as the
start of a function declaration and the second set are providing an
empty arguments list and calling the function that is the result of
evaluating the function expression inside the first ser of
parenthesise.

My questions no doubt reveal either my confusion or my ignorance or
both. I would appreciate any pointers to help me overcome either.

Don't make statements about whether code does or does not work based
upon trying other code.

Richard.

.



Relevant Pages

  • Re: C++ altering the wrong class members
    ... > In it's constructor Application instalises m_Params values. ... > executes, m_Params.title changes but the new value is corrupt ... > executes, nothing in m_Params alters ... > the program crashes because of the invalid data. ...
    (comp.lang.cpp)
  • Re: Finding heigth and widthof image
    ... function *and also* executes it. ... That will produce a syntax error. ... new function testit() ... My questions no doubt reveal either my confusion or my ignorance or both. ...
    (microsoft.public.scripting.jscript)
  • Re: C++ altering the wrong class members
    ... > An App Class derived from the Application Class ... > In it's constructor Application instalises m_Params values. ... App then alters some ... > executes, m_Params.title changes but the new value is corrupt ...
    (comp.lang.cpp)
  • Re: DAMAGE after client block
    ... you are causing memory damage. ... I could post the whole contents of the CProgressWnd class, ... said I've slimmed it down to the point that none of the code executes. ... The constructor is effectively "empty" because its first statement is ...
    (microsoft.public.vc.mfc)
  • C++ altering the wrong class members
    ... rGfxOptionsWnd* m_pGfxOptionsWnd; ... In it's constructor Application instalises m_Params values. ... App then alters some of the ... executes, m_Params.title changes but the new value is corrupt ...
    (comp.lang.cpp)