I already know that apply and call are similar functions which set this (context of a function). The difference is with the way we send the arguments (manual vs array) Question: But when should I use the bind() method ? jsbin Answer I created this comparison between function objects, function calls, call/apply and bind a while ago: .bind allows you
Tag: function
Function in JavaScript that can be called only once
I need to create a function which can be executed only once, in each time after the first it won’t be executed. I know from C++ and Java about static variables that can do the work but I would like to know if there is a more elegant way to do this? Answer If by “won’t be executed” you mean
Is there a way to provide named parameters in a function call in JavaScript?
I find the named parameters feature in C# quite useful in some cases. What can I use if I want this in JavaScript? What I don’t want is this: That approach I’ve already used. Is there another way? I’m okay using any library to do this. Answer ES2015 and later In ES2015, parameter destructuring can be used to simulate named
Call functions from function inside an object (object literal)
I’m learning to use object literals in JS, and I’m trying to get a function inside an object to run by calling it through another function in the same object. Why isn’t the function “run” running when calling it from the function “init”? Answer That code is only a declaration. You need to actually call the function: Demo: http://jsfiddle.net/mattball/s6MJ5/
How to make chainable function in JavaScript?
Lets imagine function like this: Usage of it would be like: I’m looking for a way to create function that’s chainable with other functions. Imagine usage: How would I do this? Answer Sure, the trick is to return the object once you’re done modifying it: http://jsfiddle.net/Xeon06/vyFek/ To make the method available on String, I’m modifying it’s prototype. Be careful not
Unlimited arguments in a JavaScript function
Can a JavaScript function take unlimited arguments? Something like this: I am trying: But this doesn’t work (output is only the first argument). Or the only way is: Thanks Answer There’s a weird “magic” variable you can reference called “arguments”: It’s like an array, but it’s not an array. In fact it’s so weird that you really shouldn’t use it
Define a global variable in a JavaScript function
Is it possible to define a global variable in a JavaScript function? I want use the trailimage variable (declared in the makeObj function) in other functions. Answer As the others have said, you can use var at global scope (outside of all functions and modules) to declare a global variable: (Note that that’s only true at global scope. If that
Is it possible to change the value of the function parameter?
I have one function with two parameters, for example function (a,b). I want to within the function, take the value of b, and replace that with c. I was thinking of something like $(b).replaceWith(c), but it didn’t work out. I know I can create a new variable within the function with the new value, but is it possible to change
How to run a function in JavaScript every time a variable changes?
Is there a way in JavaScript to have something like an event that listens to changes in a variable? so when its value is modified the event triggers and then I can call a function. To put this more into context I have a function which handles the html rendering of an array of objects, and I want that function
Is there a way to get the current function from within the current function?
Sorry for the really weird title, but here’s what I’m trying to do: As you can see, I would like to access the current function from within an anonymous function. Is this possible? Answer Yes – arguments.callee is the current function. NOTE: This is deprecated in ECMAScript 5, and may cause a performance hit for tail-call recursion and the like.