This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
if function does not exist write function - javascript
I have a situation when some function X is being called. After some postbacks this function is no longer declared, but still being called by the code, obviously i get js error saying X is not defined . (call it a bug if you wish) but
It is not under my control to not call it or to change the calling functionality.
What I would like to do is a fail safe that will declare such function if it does not exist. So the logic is:
If function not declared then declare one.
Is that possible in javascript i.e. to declare/register a function dynamically in global scope?
Thanks.
if (typeof window.functionX === 'undefined') {
window.functionX = function() {
// fallback code here
}
}
Sure it is
if(!myFunc) {
myFunc = function() {}
}
Related
This question already has an answer here:
Function call javascript [duplicate]
(1 answer)
Closed 6 years ago.
Can you explain this?
var guessWhat = function(){ console.log('Print this!!!'); };
function guessWhat(){ console.log('Print that???'); }
guessWhat();
// output: Print this!!!
Both are declared on the global scope. Why is the the second line not overriding the first? Is the second function lost in limbo?
function guessWhat(){ console.log('Print that???'); } // declaration
This is a function declaration, it is defined before any code is executed.
var guessWhat = function(){ console.log('Print this!!!'); }; // literal
This is a function literal, it is defined at run-time.
so, the function definition gets loaded first (before any code), and the function literal afterwards, which overrides the first definition, hence this behaviour.
Read more here.
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 7 years ago.
In my app, I have the following closure. It contains a var, which is initialized in $(document).ready:
var myClosure = (function() {
var thing;
$(document).ready(
function() {
thing = new ClassDefinedInSomeOtherFile();
}
)
return {
thing: thing
};
})();
As the page loads (I debug in chrome), a breakpoint placed in $(document).ready() is reached and I can see thing get assigned to an object of ClassDefinedInSomeOtherFile.
However, elements attempting to subsequently access myClosure.thing encounter errors stating that myClosure.thing is undefined (as do calls from the console to myClosure.thing). If thing was exposed by the return block in myClosure, why does it not reflect the new value assigned to it, when $(document).ready() ran?
Thanks!
you are using IIFE so gets executed immediately and return { thing: undefined}, after that when .ready event triggers, it runs and change thing, but that wont change the returned object, so you would get myClosure.thing is undefined
Solution:
$(document).ready(function() {
var myClosure = (function() {
var thing;
thing = new ClassDefinedInSomeOtherFile();
return {
thing: thing
};
})()
});
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
What is the difference between a function expression vs declaration in JavaScript? [duplicate]
(5 answers)
Closed 8 years ago.
I couldnT find an answer easily, so even if this question is a dupe, the answers donT come up using these keywords.
I want to know the difference between the different way of declaring functions in a sample app.js
var foo = function()
{
//..
}
function bar()
{
//..
}
var baz= function()
{
//..
}
function qux()
{
//..
}
// other??
I m also not clear about the scope where I can use each function. Thanks!
There are four ways to create a function in JavaScript.
Function declaration
This will create a variable foo in the current scope and assign a named function to it.
function foo () {
}
Function declarations are hoisted so it doesn't matter where, in the applicable scope, you put them. It is considered good coding practise to define them before you use them though.
Anonymous function expression
This will create a function without a name and use it in an expression. In this example it is assigned to the variable something.
something = function () {
};
Named function expression
This is the same as an anonymous function expression except that it has a name, creates a variable of that name in the scope of itself and is horribly broken in older versions of Internet Explorer.
something = function foo () {
};
Function constructor
Do not use function constructors. They are eval by another name. You can read about them on MDN if you're interested.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}
I have code with functions defined in two ways:
var retrieveData = function (statusValue, statusText)
{
...
}
function retrieveData(statusValue, statusText) {
..
}
retrieveData(1,2);
Can someone explain what the difference is. Seems that the second way of setting up the function is much simpler.
The 1st example creates a pointer to the function stored in the variable retrieveData, this way you can pass functions like any other variable and retrieve and use them dynamically. Other languages have similar constructs.