473,413 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

function Object,function statement,function 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 2095
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.Func);

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
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...
2
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
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...
9
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...
17
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...
16
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? ...
14
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 =...
7
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...
7
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...
24
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.