Javascript Perl

Doing common things for Perl developers on Javascript (I)

From Perl to Javascript relearning.

It is common in Perl using an array as the arguments of a function, this is the useful default behaviour on Perl and I found myself not being able to do it so I searched it and I found how to do it.

This is the Perl code I wanted to emulate.

use strict;
use warnings;

use feature 'say';

sub a {
    my ($arg1, $arg2) = @_;
    say "$arg1 $arg2";

my @a = ('arg1', 'arg2');

And this is the equivalent Javascript code using .apply a prototype function for implementors of Function class.

"use strict";

function a(a, b) {
    console.log(`${a} ${b}`);

let a_array = [ 'arg1', 'arg2' ];

// The first argument is null because it sends the value of this.
// If you are doing this to a object it probably should be different
a.apply(null, a_array);

By sergiotarxz

I am a software developer with high interest on free software.

Leave a Reply

Your email address will not be published. Required fields are marked *