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.
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 10 months ago.
Improve this question
I just have a super quick question about JavaScript so I'm not going to follow the general formatting of a question like usual.
I'm trying to understand some code in JavaScript, and the person who wrote it used a method that I'm not familiar with. As you see in the title, they've written it by typing the name of the function followed by a colon first, and then writing the actual function.
Example: myFunction: function () {}
Is anyone able to tell me what this means, why it's done, and what it accomplishes?
It is part of Object Methods like:
const person = {
myFunction: function() {}
};
you can invoke myfunction by using person.myFunction()
There are 3 ways of writing a function in JavaScript:
function myFunction() {} // Function Declaration
myFunction: function () {} // Function Expression
myFunction = () => {} // Arrow Function
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 1 year ago.
Improve this question
While in VS Code, I hit the tab after typing return and the VS Code Intellisense auto-completed "return" to "this.return".
I've never seen this before and cannot find any documentation on it.
Does anybody know if this is actually a thing, or if VS Code possibly lost it's marbles.
Note: the object I was working in does not have any properties or functions called "return".
Well, an object could have a property called return:
const obj = {
return: () => { console.log('↩️'); }
};
obj.return();
Were you in the context of a class that had a return property, or maybe its superclass did?
(Honestly it seems more likely VS Code was just being weird.)
You can run console.log("this", this) in most modern browsers this will return the JSON data of this (the variable this refers to the current instance of declared object FYI), and console.log(typeof(this.return)) and it will likely return function (if you get undefined just change it from this.return to return;)
Likely the object either has a property called return that is a function, or something has gone wrong in the autocomplete.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
From a functional programming view, where I must keep state. Which one of these two approaches is deemed safer? Or are they the same. Also if one is preferred over the other in FP, I'm guessing the variable reassignment.
1.
topics = {
...topics,
topic: { subscribers: [], registerCallback: callback }
};
2.
topics[topic] = { subscribers: [], registerCallback: callback }
The first approach would be create new object and then reassigning the variable. The second approach would be to mutate the object properties.
I do realise that spread does not do a deep copy, but that isn't the point of this question.
Given that the "normal" paradigm for functional programming is to keep objects immutable, i'd go with option #1:
topics = {
...topics,
topic: { subscribers: [], registerCallback: callback }
};
You want to structure your code in such a way, that a function will take an object as an input and it will return a new object, without changing the original one that was passed in.
Keep your functions pure and your objects immutable and you got your self a functional-style program!
EDIT: Take a look on this SO post, brace your self for some reading thought Click me!
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.