I came across a coding "test" that asked for code that would do the following:
Using JavaScript, implement a 'chart' function according to the example:
Note: '===' is used below to communicate the expected output.
var myBarChart = chart();
myBarChart.type() === 'line';
myBarChart.type('bar');
myBarChart.type() === 'bar';
myBarChart() === "Here's your bar chart!"; // THIS IS WHAT I CAN'T DO
var myScatterChart = chart().type('scatter');
myScatterChart() === "Here's your scatter chart!"; // THIS EITHER
It was relatively simple to implement the chart() object and the type() method. I got all the expected results. What I could not come up with, however, was a way to get myBarChart() and myScatterChart() to return a string.
I could override the object's toString() method so that something like the following would work:
console.log(myBarChart + ''); // 'bar'
But not get anything that would work in this form:
console.log(myBarChart()); // Looking for 'bar', but never got there
Basically, I could never get get whatever() to return a string while whatever.type() was a valid method.
Is there a way to do this that is just unknown to me?