4 ms·
I haven't done any JS in about 7 years, but back in the day I used to program like this: var make_person = function(name) { var say = function(what)
by TurboHaskal 8y ago
I haven't done any JS in about 7 years, but back in the day I used to program like this:
var make_person = function(name) {
var say = function(what) {
console.log(name + " says: " + what);
};
return {
name: name,
greet: function(who) {
say("Hi " + who.name + ". It's " + name + ".");
}
}
};
make_person("John").greet(make_person("Jane"));
I wonder if this was ever idiomatic.
- anonytrary 8y agoThis method is less efficient than using prototypes because the created instance will lug around an object with its own function, instead of sharing one. Sometimes I use "factories" for cases where my instance only needs one method or if the number of instances is going to be relatively small.