Calling JavaScript function with the syntax (0, myFunction)() [closed] - javascript

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.

Related

Global variable & Functions [closed]

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);

What does it mean when a function is written like "name: function ()"? [closed]

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

What is "this.return" in JavaScript? [closed]

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.

What is the difference between these ways when creating a function [closed]

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.

Is it a good practice for a function sometimes return a value sometimes not [closed]

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 8 years ago.
Improve this question
function Foo(p)
{
if(!check(p))
{
return false;
}
//do something
}
If above code is acceptable? Because Foo() sometimes will return false, but most time no value return.
If this isn't a good code, what's good?
If sometimes your function returns a value, and sometimes it returns undefined, then it will come down to how you capture what it returns, a type mismatch could halt your code.
If you just call your function like so
Foo(thisvalue);
then there will be no problems; however, if you call your function
var thisvariable = Foo(thisvalue);
then you need to be careful with how you try to use thisvariable.
Functions in javascript will always return some value. If you don't specify it it returns undefined. I think it is acceptable as long as you specify this behavior. Note that in your case, it is not a good idea to return false since you won't be able to distinguish it easily from undefined (you have to use === comparison)

Categories

Resources