473,406 Members | 2,439 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,406 software developers and data experts.

Differences between myFn=function(a) and function myFn(a) as method


// Between these two ways of creating a JS function, do they represent the
exact same thing?:
function myFn(a) {return 1;} // SAMPLE A
myFn = function(a) {return 1;} // SAMPLE B

// ... and then I assign one or the other function as a method like so:
function myObject() {
this.f = myFn;
}
// Base on the myObject, when I create new instances does it matter where
SAMPLE A or B was used above?
var o1 = new myObject();
var o2 = new myObject(); //etc...

/* For example, I might assume that using SAMPLE A results in one function
entry point being used amongst the multiple methods I created.
Would SAMPLE B end up creating copies of the function if it were used?
*/

Is there any good supporting JavaScript documentation that gets into the
nitty gritty of this question?
Thanks.
-- John
Jul 23 '05 #1
1 1515
"John" <No******************@hotmail.com> writes:
// Between these two ways of creating a JS function, do they represent the
exact same thing?:
function myFn(a) {return 1;} // SAMPLE A
myFn = function(a) {return 1;} // SAMPLE B
Almost (I assume this is in the global scope, or that myFn is declared
as a variable). The difference is so small that you would probably not
notice it. If used inside a function body, the function is defined before
the code of the body is executed, while the variable is only assigned
when the assignment is executed.

Try these:
---
function test1() {
alert(myFn()); // alerts 1
function myFn(){return 1;};
}
---
vs
---
function teste() {
alert(myFn()); // gives "myFn is not a function" error
var myFn = function(){return 1;};
}
---

If this difference ever matters, you should reconsider how you write
your code :)

// ... and then I assign one or the other function as a method like so:
function myObject() {
this.f = myFn;
}
They are completely equivalent as functions.
// Base on the myObject, when I create new instances does it matter where
SAMPLE A or B was used above?
No.
/* For example, I might assume that using SAMPLE A results in one function
entry point being used amongst the multiple methods I created.
Would SAMPLE B end up creating copies of the function if it were used?
*/
Whether you write
---
function myFn() {...}
function myObject() {
this.f = myFn;
}
---
or
---
var myFn = function() {...};
function myObject() {
this.f = myFn;
}
---
only one function is created for myFn.

Whether you write
---
function myObject() {
function myFn() {...};
this.f = myFn;
}
---
or
---
function myObject() {
var myFn = function() {...};
this.f = myFn;
}
---
one functions is created per call to myObject.

However, if you write:
---
for(var i=0;i<10;i++) {
function myFn(){...}
f[i]=myFn;
}
---
then only one function is created, while
---
for(var i=0;i<10;i++) {
var myFn = function(){...}
f[i]=myFn;
}
---
creates a new function for each time through the loop (although
ECMAScript implementations *may* optimize it and reuse the function).
Is there any good supporting JavaScript documentation that gets into the
nitty gritty of this question?


The ECMA262 v3 standard would be the definitive guide to nitty gritty
details:
<URL: http://www.mozilla.org/js/language/E262-3.pdf >

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

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

Similar topics

1
by: Richard A. DeVenezia | last post by:
These seem to do the same thing, beyond the source code differences, are there any differences ? function foo () { y = bar(2) return function bar (x) {return x*x} } function foo () {
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
7
by: Frank-René Schäfer | last post by:
Case: -- class X has occupies tiny amount of memory: sizeof(X) is only a little greater than sizeof(void*). -- X instantiates thousands of objects and memory does matter. -- The class has...
2
by: Lauren Quantrell | last post by:
Maybe this is just a bit too strange... Is there a way to get the name of the Function or Sub from within that Function or Sub? For example, I have the following code: Function...
8
by: WindAndWaves | last post by:
Hi everyone Why do you use the word public in a function, when the function is fully accessible without the word public in front of it. I read the help, but did not really get it. Cheers ...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
3
by: laredotornado | last post by:
Hello, What is a cross-browser way to check if a function has been defined? It seems that this code if (myFn) { myFn(); }
27
by: idoerg | last post by:
Hi all, I am running Python 2.5 on Feisty Ubuntu. I came across some code that is substantially slower when in a method than in a function. ################# START SOURCE ############# # The...
4
by: D_a_n_i_e_l | last post by:
Is there some way to implement something like a virtual function or have something like a user defined function ? One solution I thought might work: store a reference (address) of a function in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.