Skip to content
Advertisement

Tag: function

Javascript call() & apply() vs bind()?

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

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

Advertisement