3 ms·
I don't really get why you would need a createNew() method in this setup. Why not simply have a function that returns an object? function Cat() { var priva
by huskyr 15y ago
I don't really get why you would need a createNew() method in this setup. Why not simply have a function that returns an object?
function Cat() {
var private;
function meow() {
alert('meow');
}
return {
meow : meow
};
}
var cat = Cat();
- chrisbroadfoot 15y ago> Note that we did not use a lot of confusing JS stuff: no this, no new, no prototype Makes as much sense to me as it did to you. That is, very little. I wish the author would properly explain why he went to great lengths to avoid the tools available to him.
- TazeTSchnitzel 15y agoI don't know the author's reasoning, but I read Crockford's JavaScript: The Good Parts, which demonstrates several OOP styles (including this one). One of the reasons he likely didn't use the "this" keyword is because it's compromisable. "this" is anything that the invoker of the method wants it to be, so you can accidentally compromise information. Also, if you have your variables in function scope and the function is created in that scope, the keyword is unnecessary. Regarding not using new/constructor functions, some people think the non-constructor-based OOP model in JS is nicer. Finally, regarding lack of prototypes, if you want to keep secrets then you can't re-use functions. On the other hand, if you are creating public/non-privileged methods, no reason not to re-use functions.
- TazeTSchnitzel 15y agoI think it's worth noting this doesn't follow good JavaScript naming practices. Due to the disastrous effects of constructor functions if used without the new keyword, only constructor functions should have a capitalised first letter such that they can be easily distinguished.