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;
}
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 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);
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 2 years ago.
Improve this question
I have on each function inside which I am creating one object and trying to access object value but it's giving object object in return instead of it's key and value
var _field = [0, 1, 2, 3]
$(_field).each(function() {
var abc = { id: '1' };
console.log(abc);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
When you pass an object to the console log, it will implicitly call the toString method. For objects, it is best to use JSON.stringify() instead:
console.log(JSON.stringify(abc))
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"
});