Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
It would be very helpful, if someone explains the working of a curry function. I have read many examples, but not able to grasp it properly. Is it anyhow related to closure.
Currying is just technique, that can make use of any language feature (e.g. closures) to achieve the desired result, but it is not defined what language feature has to be used. As of that currying does not require to make use of closures (but in most of the cases closures will be used)
Here a little example of the usage of currying, with and without the usage of closure.
With the use closure:
function addition(x,y) {
if (typeof y === "undefined" ) {
return function (y) {
return x + y;
}
}
return x + y;
}
var additionRemaining = addition(3); // Currying
additionRemaining(5);//add 5 to 3
With the use of new Function instead of closure (partial evaluation):
function addition(x,y) {
if (typeof y === "undefined" ) {
return new Function('y','return '+x+' + y;');
}
return x + y;
}
var additionRemaining = addition(3); // Currying
additionRemaining(5);//add 5 to 3
Related
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 4 years ago.
Improve this question
I am currently learning Javascript, and have noticed that the language has numerous ways to declare a function.
Easiest (for me) would be the following:
function foo(test) {
return test + 1;
};
However, there also seem to be:
var foo = function *optional*(test){
return test + 1;
};
In this method (^), what difference does it make if you assign a name? will it basically say 'This function can be found under foo, or under optional'
Another option seems to be an 'Arrow function' that is a lot more simplified compared to the others, I understand it can also be written with curled brackets...
var foo = (test) => return test + 1;
`
var foo = (test) => {
return test + 1;
};
Also, I think you are able to explicitly say the word function in your arrow definitions, as follows:
var foo = function(test) => {
return 'test' + 1;
};
When do I use each, which one is more standard, what are the explicit differences other than how they are written, and is there anything else I should know about functions?
To answer your question. There is no major difference. However this is this interesting problem called "hoisting" that may happen that will really drive you crazy. Here is a link https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
Go out and look for any video with Douglas Crockford. He is the writer of "Javascript the Good Parts".
In addition to Hoisting mentioned above, there is another point you must get, the functions name.
Take a look at MDN Function.name, you will see what tricky can be. A good function naming is very useful and more especially when debugging.
There are two typo in your code, I fix it
var foo = (test) => return test + 1; // return already is '=>'
var foo = function(test) => { // 'function' can't go with '=>'
return 'test' + 1;
};
Should be
var foo = (test) => test + 1;
var foo = function(test) {
return 'test' + 1;
};
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 6 years ago.
Improve this question
For example I have to add 'foo' to string.
So I have at least two ways to go.
First, I can implement String.prototype.foo:
String.prototype.foo = function() {
return this + 'foo';
};
var s = 'str';
var data = s.foo(); // "strfoo"
Another way:
function foo(str) {
return str + 'foo';
}
var s = 'str';
var data = foo(s); // "strfoo"
Both look pretty. But should I think about any "underwater rocks" before choosing first or second implementation? Are there any significant reasons, such as efficiency and performance?
The first extend the functionalities of String.
Use this solution if you have a class of objects and you like to add new behaviours to it.
The second is more an utility function.
Note also that you can apply the second foo also to a variable that is not a String, so you should also test the type of the argument if you like to limit the use to a String argument.
Modifying the globals in JavaScript is always a bad decision. Don't do that, except if it's not about making a polyfill.
The implications are multiple from your code being not portable to probability of breaking someone else's code.
I would always use the second method. Or if things are really that complicated maybe implement my own class of String.
function MyString(str) {
this.str = str;
}
MyString.prototype.foo = function() { return this.str + "foo" };
var s = new MyString("str");
var data = s.foo(); // "strfoo"
Here is further reading about modifying global objects and the downsides :
https://softwareengineering.stackexchange.com/questions/104320/why-is-extending-the-dom-built-in-object-prototypes-a-bad-idea
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 years ago.
Improve this question
I just started to learn javascript and programming overall. I found these examples and trying to figure out results of those two functions.
First:
(function(){
var x = y = 11;
})();
console.log("x = " + (typeof x !== 'undefined'));
console.log("y = " + (typeof y !== 'undefined'));
Results are true and false.
Is it because var x is declared with var keyword so it is local var and y is not?
and second example:
(function(){
console.log("a");
setTimeout(function(){console.log("x")}, 1000);
setTimeout(function(){console.log("y")}, 0);
console.log("b");
})();
Please explain me the second example?
If I got it right, setTimeout will wait for execution even if time is set to 0.
Thanks
First: Correct. If you'd remove the 'var' from the 'x'-declaration that variable would also be available outside the function scope.
Second: The javascript function 'setTimeout' starts an asynchronous operation. In other words, the passed function gets added at the end of the queue to be operated at a later time, even if the passed time is 0ms.
The 'console.log' functions run synchronously, so they always will be executed before the functions given with the 'setTimeout'-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 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 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
I'm new to Javascript and am wondering if intentionally returning a value of undefined from a function is a common practice.
For example, should a divide() function be implemented like this:
var divide1 = function (x, y) {
if (y === 0) {
return undefined;
}
return x/y;
};
or like this?
var divide2 = function (x, y) {
if (y === 0) {
throw new Error("Can't divide by 0");
}
return x/y;
};
I assume that returning undefined is typically reserved for functions with no return values (equivalent of void in Java or C#).
It's the default:
function foo() {}
console.log(foo());
But it turned out it's not a good choice. Code which can run into an exceptional state shouldn't do so silently. A lot of errors in JavaScript slip through the cracks because the code doesn't break violently when something goes wrong.
So unless you're an exceptional good developer and you can guarantee that everyone who will ever touch this code is on the same level and you never make mistakes, you can return undefined. I, myself, would throw an exception.
Turns out I'm not a good-enough developer :-)
I assume that returning undefined is typically reserved for functions with no return values (equivalent of void in Java or C#).
As in Java or C#, you can also write return;.
These three functions are returning the same thing:
function VOID () {
return;
}
function VOID () {
return undefined;
}
function VOID () {
}