This question already has answers here:
How do I pass variables between functions in Javascript?
(3 answers)
Closed 6 months ago.
I am learning Javascript. I want to pass variable from one function to another. I am trying to catch option in funTwo but I do not want to declare the variable as global or use var.
function funOne(one) {
let tags = '<div class= "options">' + arr[1] + '</div>';
const option = options_list.querySelectorAll(".options")
}
function funTwo(two) {
option.classList.add("correct")
}
In javascript, variables declared with const or let are scoped: this means they exist and are accessible only in the current scope.
When you declare a variable inside a function, it is inside the function scope and inaccessible from anything that is not in that scope, including other functions. If you want to use this variable, you need to pass it either as parameter or returning it from a function call.
function b(value) {
console.log(value);
}
function a() {
const foo = 1;
b(foo);
}
or
function b() {
let value = a();
console.log(value);
}
function a() {
return 1;
}
Yes, you can return option from funOne and get it in funTwo.
function funOne(one){
let tags = '<div class = "options">' + arr[1] + '</div>'
const option = options_list.querySelectorAll(".options")
return option
}
function funTwo(two){
const option = funOne() // the function will return option.
option.classList.add("correct")
}
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:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 3 years ago.
Problem: I want to make a variable inside a function as a global variable
This works:
var x;
function myFunction() {
x = 999;
}
myFunction();
console.log(x);
But this one doesn't work while trying to declare a global variable from API Result
webOS.service.request(url, {
onSuccess: function (data) {
var serial = data.idList[0].idValue;
var udid = serial; // This is the variable that I want
callback(udid); // Trying to get this udid out of this API call
},
});
var sn;
function callback(udid) {
sn = udid; // I want this as my global variable
}
callback(udid); // produces an error which says udid not defined
console.log(sn); // undefined
How can I make var sn as global variable? Thanks in advance
It's because udid is not defined in the scope you're calling callback in - you're calling callback in your onSuccess function, so you don't need to call it again. You also need to place your console.log inside your callback function:
webOS.service.request(url, {
onSuccess: function (data) {
var serial = data.idList[0].idValue;
var udid = serial; // This is the variable that I want
callback(udid); // Trying to get this udid out of this API call
},
});
var sn;
function callback(udid) {
sn = udid; // I want this as my global variable
console.log(sn);
}
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 does 'var that = this;' mean in JavaScript?
(6 answers)
Closed 8 years ago.
I'd like to know and understand the different between this and that, and when I have to use it.
I ready many post and many tutorial but I don't understand yet
this is my class
function Container(param) {
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}
this.member = param;
var secret = 3;
var that = this;
this.service = function () {
console.log(this.member); // foo
console.log(that.member); // foo
return dec() ? that.member : null;
};
}
New
var myContainer = new Container('foo');
myContainer.service()
Calling myContainer.service() will return 'abc' the first three times it is called.
After that, it will return null
Why i have to do var that = this ??
this is a variable that gets the context of the current function (which depends on how it was called).
that has no special meaning. It is just a variable to which a value has been assigned.
In this particular case, that is assigned the value that this has while the Container is running, and is used inside the service function (but still has the value that is the context of the call to Container. Since service is a different function, its value of this could be different.
Normally, for this particular design of function, Container would be called as a constructor function (so this would be the instance object of Container) and then service would be called in the context of that instance object so you could use this instead of passing the value around via that. I have no idea why the author of that code chose to use that here.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
I have found the following code in a website .
var testModule = (function(){
var counter = 0;
return {
incrementCounter: function() {
return counter++;
},
resetCounter: function() {
console.log('counter value prior to reset:' + counter);
counter = 0;
}
};
})();
So it follows the syntax var a = (blah balh..)()
What does it actually mean? What is the meaning of variable declaration like a =()()..
It's defining a single-use function and executing it immediately. The code you provided is named the Module Pattern -- see here for more information about its properties: http://www.yuiblog.com/blog/2007/06/12/module-pattern/
A normal function might be created like this:
var f1 = function() {
console.log('bar');
};
And you could subsequently call it like so:
f1();
But in the example you provided, the function is both defined and executed once, and that function returns an object with two functions: incrementCounter and resetCounter. You can call them like so: testModule.incrementCounter() and testModule.resetCounter()
The Module Pattern is useful when you have a single object and you want to encapsulate some properties which are only available to the functions defined in the closure.
The anonymous function is executed and the return value is assigned to the variable.