I want to create a function like this, but I want the variable name
and colour
to be properties of this function, which means I just have to give them value when construct the function and do not have to declare them each time I call the function.
function A(input) {
var name = ["USA", "Japan", "India"]; // make it properties
var color = ["blue", "red", "green"]; // make it properties
var index = name.indexOf(input);
return color[index];
}
To make it clear, let me show my ideal code like this:
function A(input) {
var index = this.name.indexOf(input);
return this.color[index];
}
A.name = ["USA", "Japan", "India"];
A.color = ["blue", "red", "green"];
Is it possible to set the function some properties like object? Thanks
Finally the problem is solved by module pattern, please attach to the accepted answer.