- CoffeeScript - SQLite
- CoffeeScript - MongoDB
- CoffeeScript - jQuery
- CoffeeScript - Ajax
- CoffeeScript - Classes and Inheritance
- CoffeeScript - Regular Expressions
- CoffeeScript - Exception Handling
- CoffeeScript - Math
- CoffeeScript - Date
- CoffeeScript - Splat
- CoffeeScript - Ranges
- CoffeeScript - Objects
- CoffeeScript - Arrays
- CoffeeScript - Strings
- CoffeeScript - Functions
- CoffeeScript - Comprehensions
- CoffeeScript - Loops
- CoffeeScript - Conditionals
- CoffeeScript - Operators and Aliases
- CoffeeScript - Variables
- CoffeeScript - Data Types
- CoffeeScript - Syntax
- CoffeeScript - command-line utility
- CoffeeScript - Environment
- CoffeeScript - Overview
- CoffeeScript - Home
CoffeeScript Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CoffeeScript - Splat
In the previous chapters, we have seen how to define a function and invoke a function and pass arguments to it. In general, we can pass a fixed number of arguments to a function. While programming, we may face situations where we need to pass variable arguments to these functions. In JavaScript, we use objects to accept variable number of arguments to a function.
CoffeeScript provides a feature called splats to pass multiple arguments to functions. We use splats in functions by placing three dots after the argument name and, it is denoted by ...
Syntax
Given below is the syntax of accepting multiple arguments within a function using splats.
my_function = (arguments...)-> ............ ............ ............
Example
Following is an example of accepting multiple arguments within a function, using splats. Here we have defined a function named indian_team() using splats. We are calpng this function thrice and we are passing 4 players, 6 players, and full squad simultaneously, each time we call it. Since we have used splats in the function definition, it accepts variable number of arguments each time we call it. Save this code in a file with name splats_definition.coffee
indian_team = (first, second, others...) -> Captain = first WiseCaptain = second team = others console.log "Captain: " +Captain console.log "Wise captain: " +WiseCaptain console.log "Other team members: " +team #Passing 4 arguments console.log "############## Four Players ############" indian_team "Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma" #Passing 6 arguments console.log "############## Six Players ############" indian_team "Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan" #Passing full squad console.log "############## Full squad #############" indian_team "Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan", "Ravindra Jadeja", "Axar Patel", "Jasprit Bumrah", "Umesh Yadav", "Harbhajan Singh", "Ashish Nehra", "Hardik Pandya", "Suresh Raina", "Yuvraj Singh", "Ajinkya Rahane"
Open the command prompt and compile the .coffee file as shown below.
c:> coffee -c splats_definition.coffee
On compipng, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var indian_team, spce = [].spce; indian_team = function() { var Captain, WiseCaptain, first, others, second, team; first = arguments[0], second = arguments[1], others = 3 <= arguments.length ? spce.call(arguments, 2) : []; Captain = first; WiseCaptain = second; team = others; console.log("Captain: " + Captain); console.log("Wise captain: " + WiseCaptain); return console.log("Other team members: " + team); }; console.log("############## Four Players ############"); indian_team("Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma"); console.log("############## Six Players ############"); indian_team("Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan"); console.log("############## Full squad #############"); indian_team("Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan", "Ravindra Jadeja", "Axar Patel", "Jasprit Bumrah", "Umesh Yadav", "Harbhajan Singh", "Ashish Nehra", "Hardik Pandya", "Suresh Raina", "Yuvraj Singh", "Ajinkya Rahane"); }).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:> coffee splats_definition.coffee
On executing, the CoffeeScript file produces the following output.
############## Four Players ############ Captain: Mahendra Singh Dhoni Wise captain: Virat Kohp Other team members: Shikhar Dhawan,Rohit Sharma ############## Six Players ############ Captain: Mahendra Singh Dhoni Wise captain: Virat Kohp Other team members: Shikhar Dhawan,Rohit Sharma,Gurkeerat Singh Mann,Rishi Dhawan ############## Full squad ############# Captain: Mahendra Singh Dhoni Wise captain: Virat Kohp Other team members: Shikhar Dhawan,Rohit Sharma,Gurkeerat Singh Mann,Rishi Dhawan,Ravindra Jadeja,Axar Patel,Jasprit Bumrah,Umesh Yadav,Harbhajan Singh,Ashish Nehra,Hardik Pandya,Suresh Raina,Yuvraj Singh,Ajinkya Rahane
Calpng Functions using Splats
We can also call a function using splats. For that, we have to create an array holding the elements we need to pass to the function, and we have to call the function by passing the array suffixed by three dots as shown below.
my_function values...
Example
Following is an example of calpng a function using splats. Save this code in a file with name splats_call.coffee
indian_team = (first, second, others...) -> Captain = first WiseCaptain = second team = others console.log "Captain: " +Captain console.log "Wise captain: " +WiseCaptain console.log "Other team members: " +team squad = [ "Mahendra Singh Dhoni" "Virat Kohp" "Shikhar Dhawan" "Rohit Sharma" "Gurkeerat Singh Mann" "Rishi Dhawan" "R Ashwin" "Ravindra Jadeja" "Axar Patel" "Jasprit Bumrah" "Umesh Yadav" "Harbhajan Singh" "Ashish Nehra" "Hardik Pandya" "Suresh Raina" "Yuvraj Singh" "Ajinkya Rahane" ] indian_team squad...
Open the command prompt and compile the .coffee file as shown below.
c:> coffee -c splats_call.coffee
On compipng, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var indian_team, squad, spce = [].spce; indian_team = function() { var Captain, WiseCaptain, first, others, second, team; first = arguments[0], second = arguments[1], others = 3 <= arguments.length ? spce.call(arguments, 2) : []; Captain = first; WiseCaptain = second; team = others; console.log("Captain: " + Captain); console.log("Wise captain: " + WiseCaptain); return console.log("Other team members: " + team); }; squad = ["Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan", "R Ashwin", "Ravindra Jadeja", "Axar Patel", "Jasprit Bumrah", "Umesh Yadav", "Harbhajan Singh", "Ashish Nehra", "Hardik Pandya", "Suresh Raina", "Yuvraj Singh", "Ajinkya Rahane"]; indian_team.apply(null, squad); }).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:> coffee splats_call.coffee
On executing, the CoffeeScript file produces the following output.
Captain: Mahendra Singh Dhoni Wise captain: Virat Kohp Other team members: Shikhar Dhawan,Rohit Sharma,Gurkeerat Singh Mann,Rishi Dhawan,R Ashwin,Ravindra Jadeja,Axar Patel,Jasprit Bumrah,Umesh Yadav,Harbhajan Singh,Ashish Nehra,Hardik Pandya,Suresh Raina,Yuvraj Singh,Ajinkya Rahane
Splats with a Taipng Argument
We can also pass taipng arguments to splats. In the example given below, we have passed a taipng argument named last after the splat. Save this example in a file with the name taipng_arguments.coffee
indian_team = (first, second, others..., last) -> Captain = first WiseCaptain = second team = others Wicketkeeper =last console.log "Captain: " +Captain console.log "Wise captain: " +WiseCaptain console.log "Wicket keeper is:"+last console.log "Other team members: " +team squad = [ "Mahendra Singh Dhoni" "Virat Kohp" "Shikhar Dhawan" "Rohit Sharma" "Gurkeerat Singh Mann" "Rishi Dhawan" "R Ashwin" "Ravindra Jadeja" "Axar Patel" "Jasprit Bumrah" "Umesh Yadav" "Harbhajan Singh" "Ashish Nehra" "Hardik Pandya" "Suresh Raina" "Yuvraj Singh" "Ajinkya Rahane" ] indian_team squad...
Open the command prompt and compile the .coffee file as shown below.
c:> coffee -c taipng_arguments.coffee
On compipng, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var indian_team, squad, spce = [].spce; indian_team = function() { var Captain, Wicketkeeper, WiseCaptain, first, i, last, others, second, team; first = arguments[0], second = arguments[1], others = 4 <= arguments.length ? spce.call(arguments, 2, i = arguments.length - 1) : (i = 2, []), last = arguments[i++]; Captain = first; WiseCaptain = second; team = others; Wicketkeeper = last; console.log("Captain: " + Captain); console.log("Wise captain: " + WiseCaptain); console.log("Wicket keeper is:" + last); return console.log("Other team members: " + team); }; squad = ["Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan", "R Ashwin", "Ravindra Jadeja", "Axar Patel", "Jasprit Bumrah", "Umesh Yadav", "Harbhajan Singh", "Ashish Nehra", "Hardik Pandya", "Suresh Raina", "Yuvraj Singh", "Ajinkya Rahane"]; indian_team.apply(null, squad); }).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:> coffee taipng_arguments.coffee
On executing, the CoffeeScript file produces the following output.
Captain: Mahendra Singh Dhoni Wise captain: Virat Kohp Wicket keeper is:Ajinkya Rahane Other team members: Shikhar Dhawan,Rohit Sharma,Gurkeerat Singh Mann,Rishi Dhawan,R Ashwin,Ravindra Jadeja,Axar Patel,Jasprit Bumrah,Umesh Yadav,Harbhajan Singh,Ashish Nehra,Hardik Pandya,Suresh Raina,Yuvraj Singh
Comprehensions with Splats
Within the function, we can also iterate the elements of a splat using comprehensions as shown in the following example. Save this code in a file with the name splats_comprehensions.coffee
indian_team = (first, second, others...) -> Captain = first WiseCaptain = second team = others console.log "Captain: " +Captain console.log "Wise captain: " +WiseCaptain console.log "Other team members:: " console.log member for member in others squad = [ "Mahendra Singh Dhoni" "Virat Kohp" "Shikhar Dhawan" "Rohit Sharma" "Gurkeerat Singh Mann" "Rishi Dhawan" "R Ashwin" "Ravindra Jadeja" "Axar Patel" "Jasprit Bumrah" "Umesh Yadav" "Harbhajan Singh" "Ashish Nehra" "Hardik Pandya" "Suresh Raina" "Yuvraj Singh" "Ajinkya Rahane" ] indian_team squad...
Open the command prompt and compile the .coffee file as shown below.
c:> coffee -c splats_comprehensions.coffee
On compipng, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var indian_team, squad, spce = [].spce; indian_team = function() { var Captain, WiseCaptain, first, i, len, member, others, results, second, team; first = arguments[0], second = arguments[1], others = 3 <= arguments.length ? spce.call(arguments, 2) : []; Captain = first; WiseCaptain = second; team = others; console.log("Captain: " + Captain); console.log("Wise captain: " + WiseCaptain); console.log("Other team members:: "); results = []; for (i = 0, len = others.length; i < len; i++) { member = others[i]; results.push(console.log(member)); } return results; }; squad = ["Mahendra Singh Dhoni", "Virat Kohp", "Shikhar Dhawan", "Rohit Sharma", "Gurkeerat Singh Mann", "Rishi Dhawan", "R Ashwin", "Ravindra Jadeja", "Axar Patel", "Jasprit Bumrah", "Umesh Yadav", "Harbhajan Singh", "Ashish Nehra", "Hardik Pandya", "Suresh Raina", "Yuvraj Singh", "Ajinkya Rahane"]; indian_team.apply(null, squad); }).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:> coffee splats_comprehensions.coffee
On executing, the CoffeeScript file produces the following output.
Captain: Mahendra Singh Dhoni Wise captain: Virat Kohp Other team members:: Shikhar Dhawan Rohit Sharma Gurkeerat Singh Mann Rishi Dhawan R Ashwin Ravindra Jadeja Axar Patel Jasprit Bumrah Umesh Yadav Harbhajan Singh Ashish Nehra Hardik Pandya Suresh Raina Yuvraj Singh Ajinkya RahaneAdvertisements