This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Return Global Variable from Javascript Method
I have this.
var thisData = "";
function calculateThings(newData) {
thisData = newData.things.otherthings //has a value of 10;
}
alert(thisData) //returns nothing
What am I doing wrong?
you need to call your function:
calculateThings(newData);
should be more like:
var thisData = "";
function calculateThings(data) {
thisData = data.things.otherthings //has a value of 10;
}
calculateThings(newData);
alert(thisData) //returns nothing
where data is your parameter, and you can pass whatever you want into it.
You created a function but never call it. You need to call it via:
var thisData = "";
function calculateThings(newData) {
thisData = newData.things.otherthings //has a value of 10;
}
alert(calculateThings(thisData));
or you can self-invoke the function like:
(function calculateThings(newData) {
thisData = newData.things.otherthings //has a value of 10;
})()
Related
This question already has an answer here:
Why can’t I assign values to a variable inside a named function expression with the same name?
(1 answer)
Closed 3 years ago.
var functionVariable = function functionExpressionName() {
functionExpressionName = 1;
console.log(functionExpressionName) // function
};
functionVariable();
If You run this example you can see we can not reassign to functionExpressionName anything.
But this is also interesting we can reddeclare functionExpressionName and after this we can assign anything to functionExpressionName
var functionVariable = function functionExpressionName() {
function functionExpressionName() {
}
functionExpressionName = 1;
console.log(functionExpressionName); // 1
};
functionVariable();
If you enable strict mode, the error becomes a bit clearer:
'use strict';
var functionVariable = function functionExpressionName() {
functionExpressionName = 1;
console.log(functionExpressionName) // function
};
functionVariable();
Uncaught TypeError: Assignment to constant variable
The function name is un-reassignable inside the function, but you can create a new variable with the same name inside the function body. One way of looking at it is that the function name is declared with const just outside the function body:
var functionVariable = (() => {
const functionExpressionName = function () {
functionExpressionName = 1; // Clearly wrong - functionExpressionName is a const
// but it would work if you declared a *new* variable,
// which has a different lexical binding
console.log(functionExpressionName) // function
};
return functionExpressionName;
})();
functionVariable();
This isn't exactly what happens, but it's pretty close.
This question already has answers here:
call a javascript function using string name
(2 answers)
Closed 5 years ago.
How to call JavaScript or jquery function using variable.
var fnName= "abc"; //how to use fnName as a function call where "abc" will be function name
function abc(){ //definition........ }
Define function globally.
First Way
Call as window[functionName]().
function abc() {
alert('test');
}
var funcName = 'abc';
window[funcName]();
Second Way Add function to defined object.
function parentFunc(name) {
var childFuncs = {
"abc": function() {
alert("test");
}
}
childFuncs[name]();
}
var funcName = 'abc';
parentFunc(funcName);
This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 6 years ago.
Im working on a project, and in an attempt to shorten the amount of code I wrote a function to call all of them at once. It looks something like this function Function(peram1) {peram1 = peram1 += 5}; And I call it like so Function(someVariable) My issue is that I need to change the variable someVariable from inside the function, even though the value of peram1 is simply just the value of someVariable, but cant directly change it. I call multiple of these functions, so I cant simply just call the actual variable name from within.
var someVariable = 5;
var someSeperateVariable = 8;
function Function(peram1) {
peram1 = peram1 += 5;
};
Function(someVariable);
Function(someSeperateVariable);
console.log(someVariable, someSeperateVariable);
You can use an object to store variables, pass property name and object to function
var obj = {someVariable:5, someSeparateVariable:8};
function fn(prop, obj) {
obj[prop] += 5;
};
fn("someVariable", obj);
fn("someSeparateVariable", obj);
console.log(obj.someVariable, obj.someSeparateVariable);
Consider returning a value from Function
var peram1 = Function(Peram1);
Another option in case peram1 is not an object - is to wrap peram1 with an object:
var objContainPeram1 = { peram1: peram1 }
Function (objContainPeram1) {
objContainPeram1.peram1 = objContainPeram1.peram1 + 5
}
Another solution would be to wrap your code in a function to create a closure:
function closure() {
var someVariable = 5;
var someSeperateVariable = 8;
function foo() {
someVariable += 5;
};
console.log(someVariable)
foo()
console.log(someVariable)
}
closure()
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
var MyFunction = function () {
this.myVar = 'something';
var myVar = 'else';
};
What are the pros and cons of the two above, I see them being used.
The main difference is following:
var myFunc = function(){
this.x = 2; // You want this to be used outside of the declaration
}
var y = new MyFunc();
console.log(y.x); // 2
var myFunc2 = function(){
var x = 3; // you only want to use it inside this function
}
var z = new MyFunc2();
console.log(y.x); // undefined
So the this. variable will be accessible and the var won't be.
Use this.myVar when you want to indicate that it is a variable that acts as a property of MyFunction.
Consider:
var MyFunction = function () {
this.myVar = 'something';
var myVar2 = 'else';
};
then
myF = new MyFunction()
Note that you can now access myF.myVar (as property), but not myVar2.
This question already has answers here:
Way to differentiate between declared variable vs undeclared [closed]
(11 answers)
Closed 9 years ago.
Possible Duplicate
Way to differentiate between declared variable vs undeclared
I want to explain clearly my problem here.
I have this condition.
if(//condition1)
var x1 = 1;
else if(//condition2)
var x2 = 2;
else if(//condition3)
var x3 = 3;
This way I can have only one variable declared.
Now I want to print the variable which is declared.
How can I do that?
This is a more realistic example of what I am trying to do:
var d_pattern=/d{1}/;
var dd_pattern=/d{2}/;
var ddd_pattern=/d{3}/;
var dddd_pattern=/d{4}/;
if(dddd_pattern.test(formatString))
var dddd=this.getDayName();
else if(ddd_pattern.test(formatString))
var ddd=this.getDayNameAbbr();
else if(dd_pattern.test(formatString))
var dd=this.getDateFormatted();
else if(d_pattern.test(formatString))
var d=this.getDate();
Use another variable to hold the information:
var which;
if (condition1) {
var x1 = 1;
which = 'x1';
} else if (condition2) {
var x2 = 2;
which = 'x2';
} else if (condition3) {
var x3 = 3;
which = 'x3';
}
console.log('Declared variable is: ' + which);
But remember, as explained in several answers in your earlier question, hoisting causes all the variables to be declared.
If you want to be able to use which to show the value of the variable, it would be better to use an object, with the name as the property:
var obj = {}, which;
var which;
if (condition1) {
obj.x1 = 1;
which = 'x1';
} else if (condition2) {
obj.x2 = 2;
which = 'x2';
} else if (condition3) {
obj.x3 = 3;
which = 'x3';
}
console.log('Declared variable is: ' + which + ' value is: ' + obj[which]);
Why not use an Object?
var obj = {};
if(//conditon){
obj.x1 = 1;
}
else if(//condition){
obj.x2 = 2;
}
console.log(Object.keys(obj))
Note: this is the same logic that was given on the last v1 if this question
You can use typeof
if(typeof x1 !== "undefined"){
// ...
}
// other checks
You can iterate the context object by name. In global context its window, so you can iterate window and check
if(window[var_name] === undefined)
Regarding your question, there`s one more way to cehck if variable was defined for any context - memoize all variables, then on every change to the context write new list of variables were defined. But it will require to keep list of variables defined for the context you need to track