Open In App

Explain the use of splats with tailing argument in CoffeeScript

Last Updated : 25 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

CoffeeScript is a lightweight language that compiles into JavaScript. It provides simple and easy-to-learn syntax avoiding the complex syntax of JavaScript. CoffeeScript is influenced by JavaScript, Ruby, YAML, Haskell, Perl, Python and has influenced MoonScript, LiveScript, and JavaScript.

Installation of CoffeeScript:

Install locally for a project, use the following command:

npm install --save-dev coffeescript

To Globally install to execute the .coffee files anywhere, use the following command:

npm install --global coffeescript

When you do not know how many arguments you pass to the function call and how many parameters the function will take then we use splats in CoffeeScript. In other programming languages, this functionality is called or named variable-length arguments. 

A parameter with the splat allows us to work with an undefined number of arguments. so whenever you wanted to call your function with a varying number of arguments simply use splats. 

Before using splats with tailing arguments we will take a brief about splats. 

Syntax:

function_name = (parameter1, parameter2, parameter3...) -> 
    var_name : parameter1
    var_name : parameter2

function_name("argument1", "argument2","argument3", "argument4")

Example 1: Without tailing

Javascript




school = (first, second, third...) -> 
    std1 : first
    std2 : second
    std3 : third
                            
  
console.log"#4 names"
console.log(school("akash", "manoj","devendra", "geeks"))
  
console.log"#5 names"
console.log(school "ajay", "atul", "vishal", "madhur", "yash")


Output: 

[ '#4 names' ]
{ std1: 'akash', std2: 'manoj', std3: [ 'devendra', 'geeks' ] }
[ '#5 names' ]
{ std1: 'ajay', std2: 'atul', std3: [ 'vishal', 'madhur', 'yash' ] }

In the above example, the function call receives four arguments and the function body has only three parameters so the first three arguments will be stored in the first three arguments, and the remaining variable type arguments will store in the third variable along with that previous argument as a list.

Now let us see splat with tailing arguments. 

Syntax:

function_name = (parameter1, parameter2, 
  parameter3..., parameterlast) ->
    block1: 
        var_name1: parameter1
        var_name2 : parameter2
    block2: 
        var_namelast = parameterlast
            
function_name("argument1", "argument2",
  "argument3", "argument4",argument5)

Example 2: With trailing

Javascript




school = (first, second, third..., last) -> 
    secondary:   
        std1 : first
        std2 : second
        std3 : third
                   
    highersecondary:   
        std4 : last
               
console.log"argument with tailing "                     
console.log(school("chaitanya", "aditya",
  "swarop", "gaurav", "rupesh", "tanay"))


Output:

[ 'argument with tailing ' ]
{
  secondary: {
    std1: 'chaitanya',
    std2: 'aditya',
    std3: [ 'swarop', 'gaurav', 'rupesh' ]
  },
  highersecondary: { std4: 'tanay' }
}

In the above example By adding an ellipsis (…) next to no more than one of a function’s arguments, CoffeeScript will combine all of the argument values not captured by other named arguments into a list. It will serve up an empty list even if some of the named arguments were not supplied.

Reference: https://coffeescript-cookbook.github.io/chapters/functions/splat_arguments



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads