Javascript: Inconsistency when assigning a named function to a variable (named function expression)



Can anyone explain the difference in behaviour between Internet Explorer and Firefox in regards to the below:


var myNamespace = (function () {
var exposed = {};
exposed.myFunction = function myFunction () {
return "Works!";
}
console.log(myFunction());
// IE: "Works!"
// Firefox: ReferenceError: myFunction is not defined
console.log(exposed.myFunction());
// IE: "Works!"
// FF: "Works!"
return exposed;
})();
console.log(myNamespace.myFunction());
// IE: "Works!"
// FF: "Works!"

In interne


Related to : Javascript: Inconsistency when assigning a named function to a variable (named function expression)
Javascript: Inconsistency when assigning a named function to a variable (named function expression)
Development Tools & Services

Can anyone explain the difference in behaviour between Internet Explorer and Firefox in regards to the below:


var myNamespace = (function () {
var exposed = {};
exposed.myFunction = function myFunction () {
return "Works!";
}
console.log(myFunction());
// IE: "Works!"
// Firefox: ReferenceError: myFunction is not defined
console.log(exposed.myFunction());
// IE: "Works!"
// FF: "Works!"
return exposed;
})();
console.log(myNamespace.myFunction());
// IE: "Works!"
// FF: "Works!"

In interne

Calling a JavaScript function named in a variable
Development Tools & Services

I have a JavaScript variable which contains the name of a JavaScript function. This function exists on the page by having been loaded in and placed using $.ajax, etc.


Can anyone tell me how I would call the javascript function named in the variable, please?


The name of the function is in a variable because the URL used to load the page fragment (which gets inserted into the current page) contains the name of the function to call.


I am open to other suggestions on how to implement this solution.


What determines if a JavaScript function is a named anonymous function versus a, um, regular function?
Development Tools & Services

Reading "A re-introduction to JavaScript" I noticed something interesting about functions:


The name provided to an anonymous function as above is(or at least should be) only available to the function's own scope.


Entering some things based on the code in the tutorial at the nodejs prompt I was able to verify that node agrees with the author:


function add(foo, bar) {
return foo + bar;
}
add(1, 2);

gets me 3, and:


var five = (function plus(foo, bar) {
return foo + bar;
})(2, 3);
plus(2, 3);
Why would you assign a function to a variable instead of declaring a named function?
Development Tools & Services

Why would I do this:


var myfunc = function() { /* code */ };
...
myfunc();

instead of this:


function myfunc() { /* code */ }
...
myfunc();

Are there any benefits of using one over the other? I have seen both examples used in different places.


why would I return a named function (object) from a javascript function?
Development Tools & Services
Maybe I'm missing something obvious ... I've just started working with JavaScript, and when looking through an article about unit testing JavaScript with Jasmine, I came across this code:function Convert(number, fromUnit) { var conversions = { distance : { meters : 1, cm : 0.01, feet : 0.3048, inches : 0.0254, yards : 0.9144 }, volume : {
Calling a function named in a variable.
Development Tools & Services
I looked. I really did. Anyway...
Is it possible in c++ to call a function the name of which is contained in a variable?
Psuedo code (which is a euphemism for I can't write code that works):
Code:
double MYROUTINE(double x){
//  do stuff
}
double MYBOSSESROUTINE(const char* z)
{
 // value of z is "MYROUTINE"
    call z // <== obviously that doesn't work
}
Thanks
John

Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .