This question already has answers here:
How do JavaScript closures work?
(86 answers)
Two sets of parentheses after function call
(4 answers)
Closed last year.
source code is here. The part I'm having trouble with is this:
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const forbidden = nameRe.test(control.value);
return forbidden ? {forbiddenName: {value: control.value}} : null;
};
}
The part I don't understand is the return part. We're returning a function within a function, and it confused me a lot. I've looked around a bit but haven't been able to find any references to this topic. can you help me?
Related
This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
Why does this forEach return undefined when using a return statement
(5 answers)
Short circuit Array.forEach like calling break
(30 answers)
Closed 8 days ago.
I'm learning the basics of javascript and I ran into this exercise which confused me a bit. The exercise is to manually create a method like "includes." So when I implement it like that:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(arrr.includes(2));
it works.
but when I implement it like that, which makes much more sense to me:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(includes(2,arrr));
, it doesn't and the output is "undefined."
Can someone explain me the logic behind it? the first way doesn't even matches the functions arguments and works, while the second way does match the arguments and doesn't.
Thanks.
This question already has answers here:
Can a method be a pure function?
(1 answer)
Can function which calls pure function can be called pure function?
Can a pure function call external function?
(3 answers)
Is a nested pure function still a pure function?
(3 answers)
Closed 11 months ago.
Consider
class Service {
methodA(input: string[]): number {
const resB = this.methodB(input);
return resB * 2;
}
methodB(input: string[]): number {
return input.length;
}
}
If MethodB is a pure function, can MethodA also be considered a pure function?
What about the case, when MethodB is a pure function from an injected class?
class Service {
constructor(helper:Helper){}
methodA(input: string[]): number {
const resB = this.helper.methodB(input);
return resB * 2;
}
}
In both cases I argued yes in my team, because both cases no side effects takes place and the input>output is deterministic.
On the other side, let alone the the fact, that MethodA was using this. inside its function body was enough for other to say no.
This question already has answers here:
What is return new function(); in JavaScript? [duplicate]
(1 answer)
Is it right to think of a Javascript Function Expression that uses the 'new' keyword as 'static'
(3 answers)
Closed 3 years ago.
I've seen this a few times - in a top level function an old programmer at my job used to do the following:
function x() {
return new function() {
//some code
}
}
What is the difference between that and:
function x() {
return function() {
//some code
}
}
Wouldn't returning an anonymous function without new instantiate a new function every time? It's not currying, so I'm not sure I see a difference.
Thanks
The first function return a value of type "object" and the second return a value of type "function".
I think this piece of code can help you
console.log(typeof(function(){})) // "function"
console.log(typeof(new function(){})) // "object"
Two type of differents value so the behavior is totally different.
This question already has answers here:
Rules for the use of angle brackets in TypeScript
(2 answers)
Closed 3 years ago.
I have just seen a piece of code for React that has a syntax I have never seen before. I haven't been able to find what it actualy is. Can someone, please, explain what calling a function with <> instead of () does?
const ConfirmationServiceContext = React.createContext<
// we will pass the openning dialog function directly to consumers
(options: ConfirmationOptions) => Promise<void>
>(Promise.reject);
The piece of code is from here
This is actually Typescript type firm for an async lamda.
React.createContext<(options: ConfirmationOptions) => Promise<void>>(Promise.reject);
React.createContext is a Generic type, the < and > is how we pass the concrete type we're going to use in this instance. Here we're passing an inline function that gets a ConfirmationOptions object and returns a Promise whose value is void
This question already has answers here:
declare function name in object, Why? [duplicate]
(3 answers)
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
First of all, sorry for bad title or description, I'm not completely good with English.
I want to know what is difference between this line of code:
var obj = {
...
func: function func() { ... },
...
}
and this:
var obj = {
...
func: function() { ... },
...
}
What is it special in naming a method twice? I saw both of these ways in a single JavaScript source code. Here you can take a look at source if it's needed.
Edit: Question is not about anonymous or non-anonymous function declaring, but about functions inside objects that are called methods.
One of the biggest (and most helpful) differences is that the non-anonymous function will provide the function name in stack traces.
The named version can be used recursively as Teemu points out.