summaryrefslogtreecommitdiffhomepage
path: root/javascript.html.markdown
diff options
context:
space:
mode:
authorJoel Birchler <joel@paloalto.com>2013-12-26 22:50:17 -0800
committerJoel Birchler <joel@paloalto.com>2013-12-26 22:50:17 -0800
commit7927c1bf3a57538d39bfa3c5c5385b2cd8d84a91 (patch)
tree083416e783f467b8111f58b70373105ef11f39be /javascript.html.markdown
parent0aa7e32f059460a4b56f3674ab52eea20331787f (diff)
Added call, apply, bind JavaScript examples
Diffstat (limited to 'javascript.html.markdown')
-rw-r--r--javascript.html.markdown31
1 files changed, 31 insertions, 0 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown
index 4584a28c..7fb7ba55 100644
--- a/javascript.html.markdown
+++ b/javascript.html.markdown
@@ -319,6 +319,37 @@ var myOtherFunc = function(){
myObj.myOtherFunc = myOtherFunc;
myObj.myOtherFunc(); // = "HELLO WORLD!"
+// We can also specify a context for a function to execute in when we invoke it
+// using 'call' or 'apply'.
+
+var anotherFunc = function(s){
+ return this.myString + s;
+}
+anotherFunc.call(myObj, " And Hello Moon!"); // = "Hello World! And Hello Moon!"
+
+// The 'apply' function is nearly identical, but takes an array for an argument list.
+
+anotherFunc.apply(myObj, [" And Hello Sun!"]); // = "Hello World! And Hello Sun!"
+
+// This is useful when working with a function that accepts a sequence of arguments
+// and you want to pass an array.
+
+Math.min(42, 6, 27); // = 6
+Math.min([42, 6, 27]); // = NaN (uh-oh!)
+Math.min.apply(Math, [42, 6, 27]); // = 6
+
+// But, 'call' and 'apply' are only temporary. When we want it to stick, we can use
+// bind.
+
+var boundFunc = anotherFunc.bind(myObj);
+boundFunc(" And Hello Saturn!"); // = "Hello World! And Hello Saturn!"
+
+// Bind can also be used to partially apply (curry) a function.
+
+var product = function(a, b){ return a * b; }
+var doubler = product.bind(this, 2);
+doubler(8); // = 16
+
// When you call a function with the new keyword, a new object is created, and
// made available to the function via the this keyword. Functions designed to be
// called like that are called constructors.