This question already has answers here:
Skip arguments in a JavaScript function
(9 answers)
Skipping optional function parameters in JavaScript
(4 answers)
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
Closed last month.
I have a function that takes multiple parameters where I give each parameter a default value. When I call this function I only want to change a specific parameter. Is this possible to do in JavaScript? I was unable to find a clear answer in a Google search.
I understand that I can use several 'if' statements within my function however I am able to solve this problem in Python through the example:
setValues(val2="new")
This does not work in JavaScript being that the assignment occurs to the first positional variable in the function rather than the one explicitly chosen.
I have the code:
function setValues(val1="a", val2="b", val3="c")
{
/*
Print the value of each variable.
*/
}
setValues(val2="new");
Output:
val1 = "new"
val2 = "b"
val2 = "c"
Expected:
val1 = "a"
val2 = "new"
val3 - "c"
Related
This question already has answers here:
How can I pass variable into an evaluate function?
(7 answers)
How do i return a value from page.evaluate() in puppeteer?
(3 answers)
Closed 1 year ago.
I'm kind of new to JavaScript but I'm using a function that takes another arrow function as a parameter. All I need to do in the function is simply set a value to some variable I have declared before it. From my understanding, var means it has global scope, so I'm not really sure why I cant use the variable. If I try to pass it in the parameter I get undefined. I'll paste the 2 lines of code giving me issues
var thing = events[i]["Address"];
await page.evaluate( () => document.querySelector('[class="form-control tt-input"]').value = thing)
This question already has answers here:
Javascript: Object Literal reference in own key's function instead of 'this'
(5 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 1 year ago.
Apologies if the terminology is off - javascript isn't my thing.
I have a function:
var input = {
getVal: function () {
return $(this).attr("data-current-val");
},
setVal: function () {
$(this).attr("data-current-val", $(this).val());
},
valHasChanged: function () {
return $(this).val() !== $(this).attr("data-current-val");
}
};
As you can see there is some repetition. Firstly, I repeat the data attribute name several times; secondly, I select a jquery object numerous times.
I've tried to remove the repetition. For instance, in respect of the data attribute, by adding node: "data-current-val" at the top and then calling it with this.node in place of the string. That causes an error, as does trying to define and then use the jquery objectin the same way.
Similarly part of the boolean in valHasChanged $(this).attr("data-current-val"), logically could be replaced by this.getVal but that doesn't seem to work either. Where am I going wrong?!
Any help appreciated.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I'm trying to create a function that will do the following:
I have some text-boxes and each of them is linked to a variable with a name like:
"textbox1", "textbox2" and so on.
My function needs to change the value of a textbox, when a (change) event is triggered.
Depending on where the event happens, I want to change the value of a specific textbox.
In words, my function would do this:
func foo(newVal: string, textboxNumber: number) {
this.textbox + textboxNumber = newVal;
}
so basically, construct the textbox variable name (ie. textbox1), and the correct number is passed as argument to the function.
I just don't know how to construct the name of the variable and make it usable!
I know I could for example use CASES, so for each different case, I would use the correct value:
SWITCH (myCase) {
case 1 : this.textbox1 = newVal;
}
but the number of textbox is dynamic and growing, so I would like a solution like mine, where I decide in the argument of the function which textbox is going to get updated.
You can add dynamic object properties using the bracket syntax instead of dot syntax:
function foo(newVal: string, textboxNumber: number) {
this['textbox' + textboxNumber] = newVal;
}
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:
Get variable name. javascript "reflection"
(4 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 8 years ago.
I was wondering if it's possible to get the name of a variables in javascript or JQuery.
Suppose that I declare a variable in javascript like:
var customerNr = "456910";
If a function receive a variable, how can i return the name of the variable?
For example:
function getNameOfVariable(someVariable){
//return name of someVariable;
}
If I call this function like this:
getNameOfVariable(customerNr);
The function has to return a string whose value is "customerNr".
How can I do this in jquery or javascript? Some kind of reflection?
That is simply not possible!
The passed parameter doesn't even have to have a name. It could be a return value of a function or a raw literal (like "string" or 3).
No, this is not possible. Only values are transferred for primitive data types, not references, so how would you know what the "name" of the variable is? For example:
var customerNr="456910";
var customerNrAfterProcessing=customerNr;
getNameOfVariable(customerNrAfterProcessing);
Would it return customerNrAfterProcessing or customerNr?
However, you can imitate this behavior with objects.
var variableName = someMethodThatDeterminesVariableNameOrJustAConstantIfYouPrefer();
var myVariables={};
var myVariables[variableName]={};
myVariables[variableName].value="456910";
myVariables[variableName].variableName=variableName;
function getNameOfVariable(someVariable){
return someVariable.variableName;
}
getNameOfVariable(myVariables[variableName]);
Obviously, this amounts to a lot more work than just using the variableName variable directly, but it could be necessary in some more complicated situations.
See working with objects in JS reference