0

What is the difference between this:

Library1 = function () {};
Library1.prototype.myFunc = function (p) {
    function helper1(p) {return p * 2; }
    function helper2(p) {return p * 4; }
    this.result = helper1(p) + helper2(p);
}

and this:

Library2 = function () {};
Library2.prototype.myFunc = function(p) {
    this.result = Library2.helper1(p) + Library2.helper2(p);
}
Library2.helper1 = function(p) {return p * 2; }
Library2.helper2 = function(p) {return p * 4; }

I get the same results either way:

test = new Library1();
test.myFunc(2);
window.console.log(test.result); // 12

test = new Library2();
test.myFunc(2);
window.console.log(test.result); // 12

Is one method preferred over the other?

This post implies that method 1 "pollutes" the prototype: What is the proper way to declare javascript prototype functions calling helper functions.

Do function declarations in a prototype pollute the prototype whereas assigning them seperately is somehow cleaner?

Community
  • 1
  • 1
ciso
  • 2,887
  • 6
  • 33
  • 58
  • 1 is private via closure, 2 is public. – elclanrs Mar 11 '14 at 01:09
  • No, the declerations are private and do not pollute the prototype in any way as they are not attached to the object, I'd say attaching the functions as properties of the object pollutes the object/namespace more. Then there's garbage collection etc. but that's generally not an issue. – adeneo Mar 11 '14 at 01:13
  • 1
    I'd suggest investigating the revealing module pattern before you go much further with those helper functions. – mccainz Mar 11 '14 at 01:17
  • Such emotive language, "*pollute the prototype*". Where methods are designed for a particluar purpose (i.e. *ad hoc* methods) and are meant for internal use (because they aren't robust or to handle general cases) then they should not be exposed as public methods. That's common sense, not "*anti–pollution*". ;-) – RobG Mar 11 '14 at 01:23

1 Answers1

3

Do function declarations inside a prototype method pollute the prototype?

No, because they are private.

Is assigning auxiliary functions as methods of the constructor cleaner?

No, because this way you pollute the constructor.

Is one method preferred over the other?

If you don't want to pollute objects, better use function declarations inside methods.

The disadvantage of that is that each time you call myFunc, both helper1 andhelper2 must be recreated.

Then, if you don't want to pollute anything and don't want to recreate auxiliary methods each time, you can use

Library1 = (function() {
    function Library1() {}
    function helper1(p) {return p * 2; }
    function helper2(p) {return p * 4; }
    Library1.prototype.myFunc = function (p) {
        this.result = helper1(p) + helper2(p);
    }
    return Library1;
})();
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • 1
    "Yes, the second code pollutes the prototype" -- But it doesn't, it pollutes the constructor. – elclanrs Mar 11 '14 at 01:17
  • I think "if you don't want to pollute prototype and don't want to recreate auxiliary methods each time" is exactly what I want. I would like be efficient, not recreate functions, and not cause objects to have to search through functions on the prototype chain that are only intended as helper functions to prototype functions. – ciso Mar 11 '14 at 01:23
  • Keeping helpers private is no less "polluting", it just makes things look better. ;-) +1 for suggesting closures. – RobG Mar 11 '14 at 01:26
  • @ChrisG. If you want maximum performance, it could be better placing auxiliary code inside each function. This could result in a larger code (because you won't reuse anything), but since function calls are expensive, it could be (a little bit) faster. Test if it's worth it for you. – Oriol Mar 11 '14 at 01:27
  • @elclanrs I'm not as concerned about the constructor as I am the prototype chain. – ciso Mar 11 '14 at 01:30
  • @Oriol—function calls aren't that expensive, and compilers are very good at optimising and may "in–line" simple functions to save the call. Clear, maintainable code is to be preferred in nearly every case. – RobG Mar 11 '14 at 01:48