By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

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

P: n/a

// 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
Share this Question
Share on Google+
1 Reply


P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.