Question: I currently have a need to have a set of function arguments defined as a comma separated string and passed in at a later time.

I would like my code to function somewhat as follows.

```
function bar() {
    console.log("My name is " + arguments[0].name + " and my species is " arguments[0].species);
    console.log("String 1: " + arguments[1]);
    console.log("String 2: " + arguments[2]);
}

function foo() {
    var args = "{name: 'Dean Brunt', species: 'Human'}, 'A string', 'Some other input string, oh yes'";
    bar.apply(null, args);
}

```

I would like this to produce the output to the console:

```
My name is Dean Brunt and my species is Human
String 1: A string
String 2: Some other input string, oh yes

```

However, this does not work as `Function.prototype.apply()` takes an array as its' second argument. Whilst, I have tried converting the args string to an array, I have struggled with this due to the non-trivial regex split this would incur and my attempts have been fruitless.

Could anyone help me define an appropriate regex object I could use for the split or suggest an alternative that would produce the same result.

Many thanks.

Answer: 
It's not pretty, but you can use `eval`:

```js
function bar() {
    console.log("My name is " + arguments[0].name + " and my species is " + arguments[0].species);
    console.log("String 1: " + arguments[1]);
    console.log("String 2: " + arguments[2]);
}

function foo() {
    var args = "{name: 'Dean Brunt', species: 'Human'}, 'A string', 'Some other input string, oh yes'";
    bar.apply(null, eval('[' + args + ']'));
}

foo();
```
It's not fool-proof, but it's a start.

You should begin by formatting your data to *look like* JSON, then parse it. This is the easiest way.

```js
function printInfo() {
  console.log("My name is " + arguments[0].name + " and my species is " + arguments[0].species);
  console.log("String 1: " + arguments[1]);
  console.log("String 2: " + arguments[2]);
}

function parseInfo() {
  var jsonStr = '[' + repairJson(arguments[0]) + ']';
  var jsonObj = JSON.parse(jsonStr);
  printInfo.apply(null, jsonObj);
}

// Add and repair quotes.
function repairJson(str) {
  return str.replace(/[']/g, '"').replace(/(\b\w+\b):/g, '"$1":');
}

parseInfo("{name: 'Dean Brunt', species: 'Human'}, 'A string', 'Some other input string, oh yes'");
```

---

### Bonus

This is a little outside of the scope of what you're asking, but it's the best way, if you can actually modify your data.

```js
class Animal {
  constructor(name, species) {
    this.name = name;
    this.species = species;
  }
  toString() {
    return `My name is ${this.name} and my species is ${this.species}.`;
  }
}

function print() {
  console.log([].slice.apply(arguments).join(' '));
}

function printEach() {
  [].slice.apply(arguments).forEach(arg => console.log(arg));
}

print(new Animal('Dean Brunt', 'Human'), 'A string', 'Some other input string, oh yes');
```