473,550 Members | 2,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function Object,function statement,funct ion operator

I am so confused with these three concept,who can explained it?thanks
so much?
e.g.
var f= new Function("x", "y", "return x * y");

function f(x,y){
return x*y
}

var f=function(x,y) {
return x*y;
}

Dec 28 '06 #1
4 2107
alex schreef:
I am so confused with these three concept,who can explained it?thanks
so much?
e.g.
var f= new Function("x", "y", "return x * y");

function f(x,y){
return x*y
}

var f=function(x,y) {
return x*y;
}
The new Function syntax has a bad performance and shouldn't be used in
general. It's only use is when the code of the function has to be made
in run-time.

The second one creates a function named f, this function will be
available in global scope IIRC.

The third one creates a function without name (anonymous), and assigns
it to the variable f. This way the function is in the scope of the
variable f. Note that you should place a semi-colon after an assignment,
this way:
var f=function(x,y) {
return x*y;
};
This is not required as with all javascript statements (you can do
alert(x) without semi-colon) but you should do this conventionally.
You could also do
obj.f = function(x, y){
return x*y;
};
Then, the function f will be a member of the object 'obj'.

There is also a fourth one like this:
var f=function fName(x,y){
return x*y;
};
This way, a function named fName will be create, and assigned to the
variable f. It will be in the scope of the variable f. I'm not sure what
happens to the fName function however, is it still available in global
scope?
This fourth way is useful for debugging, as the error stack will show
the names of the functions instead of anonymous.
Dec 28 '06 #2
Frederik Vanderstraeten <fr************ *********@yahoo .co.ukwrote:
This way, a function named fName will be create, and assigned to the
variable f. It will be in the scope of the variable f. I'm not sure what
happens to the fName function however, is it still available in global
scope?
No, the name fName is only available from inside the function. So it might
be useful for an anonymous recursive function or one which uses itself for
a callback.
Dec 28 '06 #3
alex escreveu:
I am so confused with these three concept,who can explained it?thanks
so much?
They have the same results.
var f= new Function("x", "y", "return x * y");
I don't like it, for me it's just a masked "eval".
function f(x,y){
return x*y
}
A _named_ function which becomes by default a local variable.
var f=function(x,y) {
return x*y;
}
An _anonymous_ function, that is forced to be local.
The only difference I found between being anonymous or not is showed bellow:
#Named function

(function Func(arg){
/* the variable "Func" just exists inside the function body, and it
also receives a name (for me stills not very useful) */
alert(Func);
}());

//but outside of the function body Func doesn't exists
alert(window.Fu nc);

The parenthesis create a kind of closure ("closure" for me is enough by
just enclosing a variable).
#Anonymous function
var Func;
(Func = function(){
/* the only way to access the own function is by using the callee or
the "Func" variable */
alert(arguments .callee);
})();
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Dec 28 '06 #4
Frederik Vanderstraeten wrote:
alex schreef:
>I am so confused with these three concept,who can
explained it?thanks so much?
e.g.
var f= new Function("x", "y", "return x * y");

function f(x,y){
return x*y
}

var f=function(x,y) {
return x*y;
}

The new Function syntax has a bad performance
Not true, there are environments where using the Function constructor is
the quickest method of creating a function object.
and shouldn't be used in general. It's only use is
when the code of the function has to be made
in run-time.
Function objects created with - new Function - do not adopt the scope
chain of the execution context in which they are created for their
[[Scope]] property, so they should be used when the closures produced by
the alternatives need to be avoided.
The second one creates a function named f, this function
will be available in global scope IIRC.
Function declarations result in the creation of a function object as a
property of Variable object for the execution context in which the
declaration appears. Thus function decelerations that appear inside
other functions are not globally available.
The third one creates a function without name (anonymous),
and assigns it to the variable f. This way the function is
in the scope of the variable f.
A function object created with either a function declaration or a
function expression has its assigned its internal [[Scope]] property
assigned the scope chain that applied at the moment of its creation. So
it would be more reasonable to say that - f - is in the scope of the new
function object.
Note that you should place a semi-colon after an
assignment, this way:
var f=function(x,y) {
return x*y;
};
Note: that as javascript has automatic semicolon insertion advice to add
semicolons in location where automatic semicolon insertion would add
them automatically is 'best practice' advice not a requirement. (It is a
best practice that I agree with, but should not be stated as anything
more.)
This is not required as with all javascript statements
(you can do alert(x) without semi-colon)
Expression statements should all be semicolon terminated, and when -
alert(x) - is followed by a new line starting with a token that cannot
form part of an expression with - alert(x) - (or a closing brace) then a
semicolon will automatically be inserted after it. On the other hand:-

alert(x)
(b = (x - 1));

- will produce a runtime error as without the semicolon at the end of
the call expression - alert(x) - the whole becomes a call expression
where an attempt is made to call the return value from - alter(x) -,
which is undefined and so not executable.
but you should do this conventionally.
You could also do
obj.f = function(x, y){
return x*y;
};
Then, the function f will be a member of the object 'obj'.
From the point of view of the created function object there is no
distinction between its being referred to by a property of the Variable
object for the current execution context and its being referred to by a
property of your - obj - object.
There is also a fourth one like this:
var f=function fName(x,y){
return x*y;
};
This way, a function named fName will be create, and
assigned to the variable f.
It will be in the scope of the variable f. I'm not sure
what happens to the fName function however, is it still
available in global scope?
The 'names' of functions created with function declarations are not
necessarily globally available so this concern is not real. Officially
(by specification) the use of an optional Identifier with a function
expression causes an extra object to be added to the scope chain of the
execution context in which it is being evaluated and the name used as a
property of that object. This extended scope chain becomes the function
object's [[Scope]] property and a reference to the function object
assigned to the named property of the object added to the scope chain.
The object added to the scope chain is then removed following the
creation of the function object. Thus the only code capable of referring
to the function using the name specified with the optional Identifier is
the code within the function body.

In practice this is an area where implementation bugs are common and
many environments create a new named property of the Variable object for
the execution context in which the function is created and assign a
reference to the newly created function to that property.
This fourth way is useful for debugging, as the error stack
will show the names of the functions instead of anonymous.
But the buggy implementations make this a potentially undesirable
trade-off against writing code that will behave differently in different
environments.

It is also interesting that you deprecate the use of - new Function -
out of some impression that it may not perform well but propose a form
of function expression that implies a runtime overhead that has no
benefits beyond debugging (and its own potential for introducing bugs
due to the leaking of the function names into containing scopes in buggy
implementations ).

An important aspect of the creation of the function objects not
mentioned here is when that creation happens (of particular relevance
when in the case of function declarations and function expressions the
internal [[Scope]] properties of the objects will be assigned the scope
china that applies at the moment of creation). Function objects created
with - new Function - and by the evaluation of function expressions are
created when the pertinent lines of code are executed, while with
function declarations the function object is created during 'variable
instantiation', prior to the execution of any statements in the
pertinent execution context. Thus:-

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

- works because the function declaration results in the creation of a
function object, and the creation of a property of the Variable object,
named 'a' and that refers to the function object, before the line of
code that calls - a - is executed. While:-

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

- does not work because the attempt to call - a - happens before any
value has been assigned to the 'a' property of the variable object.

Richard.
Dec 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
4188
by: chirs | last post by:
I am trying to understand a piece of code. In a javascrpit file, there is a function: function ItemStyle(){ var names=; addProps(this,arguments,names,true); }; In the html file, it calls the function as:
2
4764
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
27
2451
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to...
9
2020
by: Netocrat | last post by:
Any comments on the correctness of the statements 1, 2a, 2b, 3 and 4 in the code below? If they are correct, then the definition of an object as well as that of an lvalue is broken in C99 by the following reasoning: foo() does not return an object, so the return of foo() conceptually is not stored, yet we are able to obtain a pointer to...
17
1996
by: Matt Kruse | last post by:
Perl's map() function is a powerful shortcut to accomplish many "loop over an array and do X" operations. Below is a quick hack to simulate similar functionality. I've used it a few times and find it to be quite handy. Any thoughts? <script type="text/javascript"> function map(func) { var i,j,o; var results = ;
16
2431
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? -> array2 is assigned to array1 ....???? the reason being it is
14
2008
by: emailscotta | last post by:
Some of the object properties in the Dojo Toolkit are set to objects but they are using syntax like this: object.property = new function() { this.property = someValue; this.property = someFunction; } Is the property set to a new object and if so what is the "new function()" statment doing?
7
2785
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an object of that class. And is that also the reason why if you use class member functions for operators << and >you have to write: someclass <<...
7
3199
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the bastard so posting it before I forgot again... By taking this minimum code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head>
24
1891
by: Rahul | last post by:
Hi Everyone, I was just overloading operator = for a class and i have a problem in one case... class A { A& operator=(const A& obj) { return *this;
0
7560
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7487
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7758
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7517
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7844
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5126
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1978
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1093
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.