Generally speaking, in C# i have a method like this;
public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName = "",
int pageIndex = 0,
int pageSize = int.MaxValue,
bool showHidden = false)
{
//rest of the code
}
and when i need to specify the parameter order /override/call manually, i do this;
//manually saying, pageIndex should be as parameter 1 and showHidden should be true and order no 2
GetAllManufacturers(pageIndex:10, showHidden:true);
Similarly in JavaScript, i have the following function;
var CommonManager = function () {
var
displayWizard = function (wizardName, formName, leaveStepCallBack) {
leaveAStepCallback('test');
function leaveAStepCallback(obj) {
if (typeof (leaveStepCallBack) == "function") {
//yaheeeeeee callback is there
return leaveStepCallBack();
}
//oh no callback, handle the defaults
return validateSteps(); // return false to stay on step and true to continue navigation
}
};
return {
displayWizard: displayWizard
};
}();
and if i want to handle the callback i do call it like this;
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), function() {
//i handled it, don't need to call default function
});
if i don't want to handle the callback, i do like this;
CommonManager.displayWizard($('#wizard'), $('#attributeForm'), undefined);
Note that, i have several optional parameters but i skipped it here. In my orginal case, i am passing undefined, undefined, undefined
So my questions are;
1) - How do i make it adjusted to i can call it like this;
//Notice the order of the parameter
CommonManager.displayWizard(attributeForm : $('#attributeForm'), wizard: $('#wizard'));
2) - if 1 isn't possible then How do i skipped passing this undefined as calling the Orignal Callback and call it like this
CommonManager.displayWizard($('#wizard'), $('#attributeForm'));
i could use the above code directly but i have the last parameter which need to be passed as well like this;
CommonManager.displayWizard(wizard, attributeForm, undefined,undefined, true);
3) - Lastly i wanna know, if i am following the right way of doing or handling this optional parameters
Please, let me know if the question doesn't make sense.
How do i make it adjusted
You can make your function take a parameter object, like this:
CommonManager.displayWizard({ attributeForm : $('#attributeForm'), wizard: $('#wizard') });
You could combine both approaches. For instance, in jQuery these lines are equivalent:
$(this).css("color", "red");
$(this).css({ color: "red" });
This is because the css function checks if its first parameter is an object and acts accordingly.
if 1 isn't possible then How do i skipped passing
You already can use the following:
CommonManager.displayWizard($('#wizard'), $('#attributeForm') /* no undefined here */);
If you skip a parameter at the end of the argument list, the default value passed to the function will be undefined.
Lastly i wanna know, if i am following the right way of doing or handling this optional parameters
If I know I may want to expand on a function's parameters in the future and some of them are/will be optional, I often create the function so that it accepts a single parameter: an object containing all the values I need to pass to the function.
This way, I can add parameters with default values, and still have calls that skip some values without breaking anything.
Related
I've read the answers here and here that describe how to read the value of jQuery ajax settings. My question, however, is this:
if I have previously invoked the ajaxSetup function with settings, like $.ajaxSetup({cache: true}), and then I invoke $.ajaxSetup() (with no arguments), are there any side effects to this invocation?
Or more bluntly, does calling $.ajaxSetup() with no arguments do anything other than return a plain object that contains the settings for ajax?
The answers I've referenced above both call ajaxSetup in this way in order to read the value of a setting, and I have been unable to find any official answer on the jQuery docs that indicate what calling ajaxSetup with no parameters does, probably because its use is so heavily discouraged.
In my browser, I ran a simple test by loading jQuery, running $.ajaxSetup({async: false}); followed by $.ajaxSetup(); and found that the value of async was still unchanged (the default value is true, and so if calling $.ajaxSetup() resets the settings to default values, then the value of async should have been changed), but this probably isn't a strong enough evidence to conclude that side effects do not occur when the function is invoked in this way.
Does calling $.ajaxSetup() with no arguments do anything other than return a plain object that contains the settings for ajax?
No.
Example
// set a new custom url
$.ajaxSetup({
url: 'CHANGED'
});
// call it again with no params
$.ajaxSetup();
// the url is still the custom url
console.log('url: ' + $.ajaxSetup().url);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Why?
If we look at the code for ajaxSetup, we see that, when called with a single parameter, it returns the call:
ajaxExtend( jQuery.ajaxSettings, target );
where target is the single argument you passed into ajaxSetup().
Now, inside ajaxExtend, we can see that it tries to iterate over that same argument with a for in loop.
Relevant code:
function ajaxExtend( target, src ) {
for (var key in src) {
// here is where target, which in this case is jQuery.ajaxSettings, gets overriden
}
return target;
}
Since you don't pass anything, basically you are doing
function ajaxExtend( target, src ) {
for (var key in undefined) {
// this is never reached
}
return target;
}
which never enters the for in loop and returns the current value of jQuery.ajaxSettings.
I need to feed a pipe() handler function a bunch of function names so it can execute them in order, waiting for completion of each as it goes. This is great when those functions don't need parameters passing, but when parameters are needed I can't figure out how to pass them without the function going ahead and invoking itself (caused by the brackets).
For example, this is what I typically pass:
pipeHandler([function1, function2]);
It'll then invoke function1() and function2() before the promise is completed.
Where it gets difficult is when I want to do something like thiss:
pipeHandler([function1('value'), function2]);
That causes function1() to invoke immediately, completely bypassing the promise mechanism.
In case it helps, this is the handler function:
function pipeHandler(requiredFunctions) {
//Execute first required function
var executeFunctions = requiredFunctions[0]();
//Execute each subsequent required function using pipe()
for ( var index = 1; index < requiredFunctions.length; index++ ) {
executeFunctions = executeFunctions.pipe(requiredFunctions[index]);
}
//Execute allDone() using pipe()
executeFunctions = executeFunctions.pipe(allDone);
}
Hope somebody has an idea!
Why not
pipeHandler([function() { function1('value'); }, function2]);
?
This is where anonymous functions shine. If you spend some time working in Javascript, you'll probably encounter the same problem when using setTimeOut at some point.
This can be done concisely using bind. Syntax:
pipeHandler([function1.bind(scope, 'value')]);
Bind returns a partial function application, which is a new function in scope scope, with the fixed first parameter 'value'. It'll work with any number of arguments.
You can use an anonymous function, which can invoke the function1
pipeHandler([function () {;
function1('value')
}, function2]);
if you wanna pass parameters without invoking function you may do it like so :
function add (a, b) {
return a + b;
}
// Outputs: 3
console.log(add(1, 2));
// Outputs: function
console.log(add.bind(this, 1, 2));
and this will return a function
function () { [native code] }
if you wanna invoke it
// this will return 3
console.log(add.bind(this, 1, 2)());
What you're probably looking for is called 'Partial application.'
Depending on which browsers you need to support you may be able to simply use bind.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Partial_Functions
As far as I can tell from reading the question, there is no asynchronicity, just a regular single-threaded sequence of function calls, with the possibility of passing parameters at each call.
If so then you want to use jQuery.Callbacks. Your scenario is precisely what jQuery.Callbacks are for. The documentation says :
The jQuery.Callbacks() function, introduced in version 1.7, returns a
multi-purpose object that provides a powerful way to manage callback
lists. It supports adding, removing, firing, and disabling callbacks.
Having read the documentation for jQuery.Callbacks, it's probably still not obvious how to pass parameters to functions in the list.
The simplest option is to fire the list with an object that can be used by the functions in the list :
function myFunction1(obj) {
console.log(obj.myProperty1);
}
function myFunction2(obj) {
console.log([obj.myProperty1, obj.myProperty2].join());
}
var callbacks = $.Callbacks();
callbacks.add(myFunction1);
callbacks.add(myFunction2);
callbacks.fire({
myProperty1: 'X',
myProperty2: 'Y'
});
A more sophiisicated approach allows you :
to specify parameter(s) for each function as it is added to the list, and
to specify a context for all functions in the list
thus giving you two mechanisms for passing data to the functions and the freedom to specify that data in either a .add() statement or a .fire() statement, or both.
For this, you need the following utility function :
function makeClosure(fn) {
var args = Array.prototype.slice.call(arguments, 1);//seriously clever line - thank you John Resig
return function (context) {
fn.apply(context, args);
}
}
which can be used as follows :
function f1() {
console.log(this.myProperty));
}
function f2(value1, value2) {
console.log(value1 + ', ' + value2);
}
function f3(value1, value2) {
//note the use of `this` as a reference to the context.
console.log(value1 + ', ' + value2 + ', ' + this.myProperty);
}
var callbacks = $.Callbacks();
callbacks.add(makeClosure(f1, 'A1'));
callbacks.add(makeClosure(f2, 'B1', 'B2'));
callbacks.add(makeClosure(f3, 'C1', 'C2'));
callbacks.fire({
myProperty: 'Z'
});
DEMO
jQuery's $.proxy(function, context, value) is particularly helpful in this case since it:
Takes a function and returns a new one that will always have a particular context.
Therefore, not only you can change the context of the function being invoked (you can provide an object instead of this), but you can also pass as many arguments/parameters values as the function receives without invoking it directly:
function fun_w_param(v) {console.info("I'm #1, here's my value: " + v)}
function fun_no_param() {console.info("I'm #2")}
function pipeHandler(f1, f2) {
f2();
console.warn("Handling function1 with a second delay");
setTimeout(function(){f1()}, 1000);
}
// add your value below as a proxy's argument
pipeHandler(
$.proxy(fun_w_param, this, '!!!PROXY!!!'),
fun_no_param
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Running the above will delay "function1" execution and it will display the value that you provide into the proxy's parameter.
Using arrow methods you can simply do this
pipeHandler([() => function1('value'), function2]
I'm trying to call a js function within another one, but use the argument to specify the function. ie depending on the argument passed, it will call a different function
function toggle(n){
if (sessionStorage['toggle'+n]== 0){
check+n();
}
else
}
So, for example, if the argument 'Balloons' was passed as n, then it will call the function checkBalloons(). "check+n();" is not currently working here. Sorry for my lack of simple js syntax!
If the function is defined in the global scope (browser) you can do:
window["check"+n]();
or some tenants like Node.js you would access it from global object.
global["check"+n]();
if it is a part of some other object then you would do the same.
obj["check"+n]();
Functions and properties defined on an object can be accessed using [] convention as well. i.e obj["propFuncName"] will give you reference to it, so in case of methods you add () to invoke it.
If the function is global, you would do this:
window["check" + n]();
or, you could put your function in an object like so:
myNamespace = {};
myNamespace.checkSomething = function(){ /* ... */ }
// call it like this:
myNamespace["check" + n]();
The answers thus far are correct, but lack explanation.
In JavaScript, you cannot call a function by name when that name is a string. What you can do is retrieve a value from an object by name, and if that value happens to be a function, you can then call it. For example:
var myObject = {};
myObject.myFunction = function() { alert('test!'); };
// Get the property on `myObject` called `myFunction`
var theFunctionLookup = myObject['myFunction'];
// Since that property was a function, you can call it!
theFunctionLookup();
In the browser, functions that are defined in the global scope are attached to the window object. For example, this works:
function myFunction() { alert('test'); }
var theFunctionLookup = window['myFunction'];
theFunctionLookup();
You can shorten the last two lines into one:
function myFunction() { alert('test'); }
// Look up and call the function in one line.
window['myFunction']();
For the same reasons, you can use a dynamically-calculated string to look up functions.
function checkBalloon() {
alert('checking balloon');
}
function toggle(n){
if (sessionStorage['toggle'+n]== 0){
window['check' + n]();
check+n();
}
}
toggle('Balloon');
if you do this way:
if (sessionStorage['toggle'+n]== 0){
window["check" + n]();
}
will work
Ok hopefully this come across correctly. I am building a universal javascript function that will build a menu and then also build the functions that each menu item would call. To do this, I need to pass a list of the commands to be called for each option.
So for example:
var thecall = 'alert("hi, this works");';
function myfunction(thecall)
{
//In here I want to excute whatever commands is listed in variable thecall
.....
}
I'm sure doing it this way is completely stupid, but I don't know how else to do this.
Basically, I need my function to perform other functions on a variable basis.
Thanks!!
I made it a bit fancier to show you how you can use it.
var thecall = function(name){alert("hi " + name + ", this works");};
function myFunction(function_ref)
{
function_ref('Mark');
}
myFunction(thecall);
You can execute arbitrary strings of JavaScript using eval(), but that is not the best solution for you here (it's almost never the best solution).
Functions in JavaScript are themselves objects which means you can store multiple references to the same function in multiple variables, or pass function references as parameters, etc. So:
var thecall = function() {
alert("hi, this works");
};
function myfunction(someFunc) {
someFunc(); // call the function that was passed
}
myfunction(thecall); // pass reference to thecall
Note that when passing the reference to the thecall function there are no parentheses, i.e., you say thecall not thecall(): if you said myfunction(thecall()) that would immediately call thecall and pass whatever it returned to myfunction. Without the parentheses it passes a reference to thecall that can then be executed from within myfunction.
In your case where you are talking about a list of menu items where each item should call a particular function you can do something like this:
var menuItems = [];
function addMenuItem(menuText, menuFunction) {
menuItems.push({ "menuText" : menuText, "menuFunction" : menuFunction });
}
function test1() {
// do something
}
addMenuItem("Test 1", test1);
addMenuItem("Test 2", function() { alert("Menu 2"); });
// and to actually call the function associated with a menu item:
menuItems[1].menuFunction();
Notice the second menu item I'm adding has an anonymous function defined right at the point where it is passed as a parameter to addMenuItem().
(Obviously this is an oversimplified example, but I hope you can see how it would work for your real requirement.)
I think your looking for the eval function.
var code= 'alert("hi, this works");';
eval(code);
I want to write my own function in JavaScript which takes a callback method as a parameter and executes it after the completion, I don't know how to invoke a method in my method which is passed as an argument. Like Reflection.
example code
function myfunction(param1, callbackfunction)
{
//do processing here
//how to invoke callbackfunction at this point?
}
//this is the function call to myfunction
myfunction("hello", function(){
//call back method implementation here
});
You can just call it as a normal function:
function myfunction(param1, callbackfunction)
{
//do processing here
callbackfunction();
}
The only extra thing is to mention context. If you want to be able to use the this keyword within your callback, you'll have to assign it. This is frequently desirable behaviour. For instance:
function myfunction(param1, callbackfunction)
{
//do processing here
callbackfunction.call(param1);
}
In the callback, you can now access param1 as this. See Function.call.
I too came into same scenario where I have to call the function sent as parameter to another function.
I Tried
mainfunction('callThisFunction');
First Approach
function mainFuntion(functionName)
{
functionName();
}
But ends up in errors. So I tried
Second Approach
functionName.call().
Still no use. So I tried
Third Approach
this[functionName]();
which worked like a champ. So This is to just add one more way of calling. May be there may be problem with my First and Second approaches, but instead googling more and spending time I went for Third Approach.
function myfunction(param1, callbackfunction)
{
//do processing here
callbackfunction(); // or if you want scoped call, callbackfunction.call(scope)
}
object[functionName]();
object: refers to the name of the object.
functionName: is a variable whose value we will use to call a function.
by putting the variable used to refer to the function name inside the [] and the () outside the bracket we can dynamically call the object's function using the variable. Dot notation does not work because it thinks that 'functionName' is the actual name of the function and not the value that 'functionName' holds. This drove me crazy for a little bit, until I came across this site. I am glad stackoverflow.com exists <3
All the examples here seem to show how to declare it, but not how to use it. I think that's also why #Kiran had so many issues.
The trick is to declare the function which uses a callback:
function doThisFirst(someParameter, myCallbackFunction) {
// Do stuff first
alert('Doing stuff...');
// Now call the function passed in
myCallbackFunction(someParameter);
}
The someParameter bit can be omitted if not required.
You can then use the callback as follows:
doThisFirst(1, myOtherFunction1);
doThisFirst(2, myOtherFunction2);
function myOtherFunction1(inputParam) {
alert('myOtherFunction1: ' + inputParam);
}
function myOtherFunction2(inputParam) {
alert('myOtherFunction2: ' + inputParam);
}
Note how the callback function is passed in and declared without quotes or brackets.
If you use doThisFirst(1, 'myOtherFunction1'); it will fail.
If you use doThisFirst(1, myOtherFunction3()); (I know there's no parameter input in this case) then it will call myOtherFunction3 first so you get unintended side effects.
Another way is to declare your function as anonymous function and save it in a variable:
var aFunction = function () {
};
After that you can pass aFunction as argument myfunction and call it normally.
function myfunction(callbackfunction) {
callbackfunction();
}
myfunction(aFunction);
However, as other answers have pointed out, is not necessary, since you can directly use the function name. I will keep the answer as is, because of the discussion that follows in the comments.
I will do something like this
var callbackfunction = function(param1, param2){
console.log(param1 + ' ' + param2)
}
myfunction = function(_function, _params){
_function(_params['firstParam'], _params['secondParam']);
}
Into the main code block, It is possible pass parameters
myfunction(callbackfunction, {firstParam: 'hello', secondParam: 'good bye'});
callbackfunction = () => {}
callbackfunction2(){
}
function myfunction1(callbackfunction) {
callbackfunction();
}
//Exe
myfunction1(callbackfunction);
myfunction1(callbackfunction2.bind(this));
Super basic implementation for my use case based on some excellent answers and resources above:
/** Returns the name of type member in a type-safe manner. **(UNTESTED)** e.g.:
*
* ```typescript
* nameof<Apple>(apple => apple.colour); // Returns 'colour'
* nameof<Apple>(x => x.colour); // Returns 'colour'
* ```
*/
export function nameof<T>(func?: (obj: T) => any): string {
const lambda = ' => ';
const funcStr = func.toString();
const indexOfLambda = funcStr.indexOf(lambda);
const member = funcStr.replace(funcStr.substring(0, indexOfLambda) + '.', '').replace(funcStr.substring(0, indexOfLambda) + lambda, '');
return member;
}