This question already has an answer here:
Get argument count (or even names) of javascript function object
(1 answer)
Closed 11 months ago.
How can i determine if function have param or not
Like
function Foo(bar){}
And
function Foo(){}
I tried to use Foo.arguments but always return null at both
How can i know when function have param or know param length?
You can access the number of parameters a function has via .length. For example,
function Foo(bar){}
console.log(Foo.length); // 1
and
function Foo(){}
console.log(Foo.length); // 0
Related
This question already has answers here:
Can you rebind a rebound function using `bind`
(3 answers)
How to override 'this' parameter of a bound function in javascript
(3 answers)
Javascript function bind override (how to bind it to another object)
(2 answers)
Closed 4 months ago.
I have a question about Function.prototype.bind.
function f() {
return this;
}
const g=f.bind({foo:'foo'});
const h=g.bind({bar:'bar'});
console.log(h()); // expected {bar:'bar'} but get {foo:'foo'}
I was expecting the above snippet to produce either {foo,bar} or {bar} but instead I get {foo}.
Can you explain what is going on?
Using .bind() to create a function that "fixes" the value of this in the called function results in a function where this cannot be overridden.
What you get back from .bind() is a new function that will pass along arguments to the original target function, with a guarantee that in that called function the value of this will be what you ask for. Re-binding that function therefore does no good: you do get back a new function, but it calls a function (the first bound function) that explicitly ignores its own this value.
This question already has answers here:
Two sets of parentheses after function call
(4 answers)
Closed 4 years ago.
const List = connect(mapStateToProps)(connectedList);
There is a function called "connect" being called with "mapStateToProps" as the argument, however there is "connectedList" surrounded by parenthesis right after the function calling. I haven't seen that before and I didn't find anything about that in the es6 articles I read for research.
The connect function most likely returns another function that accepts one argument which is being invoked.
function getFunc(arg){
alert(arg);
return function(arg2){
alert(arg2);
}
}
getFunc("arg1")("arg2");//invokes the getFunc function and invokes the returned function
This question already has answers here:
JavaScript variable number of arguments to function
(12 answers)
Closed 5 years ago.
I would like to call a function, lets say test_func that accepts variable number of arguments. (like this function http://locutus.io/php/array/array_intersect_assoc/)
Please note I would like to avoid modifying the receiver function! All the answers so far require modifying the receiver function, just as the linked "possible dupes". I would like to programmatically generate the argument list itself. (eg pass variable number of objects)
I do not know in advance how many arguments I will have. How can I generate a variable length argument with objects?
var v1 = {test:1};
var v2 = {test2:2};
var obj_arr = [v1,v2];
console.log(test_func (obj_arr.join(",")));
//in my case this should be the equivalent of test_func (v1,v2);
function test_func (object_arg) {
return(arguments.length);
}
//should return 2!
If you're in an ES5 environment you can use arguments:
function test() {
var param1 = arguments[0];
var param2 = arguments[1];
// arguments is array like, you can iterate over it with a loop
}
If you are in an ES6 environment you can either use the rest operator as suggested by Suren or also use the arguments variant as above - depending on the convention of you're team.
This question already has answers here:
How to get function parameter names/values dynamically?
(34 answers)
Inspect the names/values of arguments in the definition/execution of a JavaScript function
(5 answers)
javascript: get names of actual arguments passed in a function
(1 answer)
Closed 5 years ago.
This is a bit of a weird situation, but say I have the following function:
function myFunction(name, age, job){...}
Is there a way to get keys / loop through the required params for it (not the argument array passed to it), so it would be something like:
for(required_param_keys_by_myFunction){
console.log(key) // --> name, age, job
}
This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
Closed 5 years ago.
Suppose I have a function as this:
function test(a, b, c, d) {
...
}
Is there a way that I can pass my inputs into the function as follows:
test(a:1,c;2)
Named values doesn't support in a function, you have 2 options:
Pass undefined value to unused variable like test(1, undefined, 2, undefined)
Modified the parameters as an object like test(obj) and use obj.a, obj.b,... to pass the values
You can not as you exactly suggested, but you could have your function take an object with your arguments as fields.
Ex:
function test(myArguments) {
if (myArguments.a) {
// do something with myArguments.a
}
...
}
Call as:
test({a: 1, b: 2});