Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
In Javascript...If a variable is defined globally and I use a function to manipulate the value of that variable - do I have to return at the end of the function or does it matter since the variable is global?
I just experimented with the situation you describe and it appears that you do not need to return the global variable from the function in order for the variable to be altered by the function. Please see the code and run it.
var myVar = 'x';
function myFunction(){
myVar = 'y';
}
myFunction();
console.log(myVar);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Hello i'm trying to access an array from a function what i'am returning work, it's an array but the result in my function "onChangeClient" always display the same "undefined". why ?
Typescript:
JavaScript (and hence TypeScript) uses lexical scope, meaning you cannot create an Array in an inner function and access it from outside. What you can do: Create the array in the outer function (getRessource) and return it from there.
async getRessource(id) {
var ref = ... elided ...
const returnArray = []; // define it here
await ref.once('value' ... elided ... );
return returnArray;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
When creating functions, What is the main difference and when to use these ways of creating a function?
onSubmit = () => {}
functionsName : () => {}
There's no difference in the creation of the functions, just what is done with the function once it's created:
Your onSubmit one creates a function and assigns it to an in-scope variable.
Your functionsName one creates a function and assigns it to an object property. This form is only valid within an object initializer. (Outside of an object initializer, it's not a syntax error, but it's just a labelled statement and the function is never assigned to anything.)
You may find another answer of mine useful as well. It's a rundown of various ways of creating functions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm very new in the JavaScript, but I found the syntax (0, myFunction)() to call anonymous functions on JavaScript, but I don't know what means the 0 before the anonymous function, also I don't know if instead of 0 I can use 1 or 2, etc.
Basically my question is what is the difference between call a function myFuntion() or (0, myFunction)(). The function is in a global context.
Here is an example.
var object = {}
object.foo = function(){}
The difference between call the function
(0,object.foo)();
or
object.foo();
You can rewrite both calls into the following equivalents:
object.foo.call(null); // (0,object.foo)();
object.foo.call(foo); // object.foo();
As you can see, the only difference is the "binding" of this inside the called function; but the use of (0, something)(); is considered as cryptic and should be avoided in a professional code base.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a JavaScript function like this:
obj.use('hello', function () {
// Here I want to access the string 'hello'
});
Is this at all possible, or are there any good workarounds that I can use?
My answer is based on the assumption that your usage indicates use as being a method of the object obj. In which case the way to achieve your objective is to use the same object to store the first parameter to obj.use.
var obj = {
use: function(greeting, fn) {
obj.greeting = greeting;
fn();
}
}
obj.use('hello', function () {
console.log(obj.greeting); //output: 'hello'
});
// Here I want to access the string 'hello'
Well that's easy, you just store the string in a variable whose scope is accessible inside the method:
var greeting = 'hello';
obj.use(greeting, function () {
console.log(greeting); // logs "hello"
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a button and I have function which should execute on click.
example:
<button id="button1">bla bla</button>
<script>
var justFunctionName = "function1";
var function1 = function()
{
alert("!");
}
$("#button1").click(justFunctionName);
</script>
HTML
<button id="button1">bla bla</button>
jQuery
var justFunctionName = "function1";
function function1()
{
alert("!");
}
$("#button1").on("click", window[justFunctionName]);
See working jsFiddle demo
$("#button1").click(justFunctionName);
should be
$("#button1").click(function1);
var justFunctionName = "function1";
This is assigning the string function1 to the variable justFunctionName.
If you were to do: console.log('justFunctionName'); you would end up with the following result:
>function1
Therefore this variable assignment is completely irrelevant for what you are hoping to achieve. Instead of just assigning a variable to a function you are assigning a variable to a variable which is assigned to a function.
Now take a look at what you are doing here:
var function1 = function () {
alert("!");
};
This is assigning a variable function1 to the function doing the alert. In this instance, think of the variable as a reference to the function. In order to have the button trigger the alert, you need to call the reference to the function (in this case function1):
$("#button1").click(function1);