0

So I know that I can fix the scope of my module class below using bind as in this answer.

The only thing is that I use a slightly diffrerent syntax for my module and I am not quite sure how to apply it?

My question is then, how do I apply bind to my function correctly so that the context of this is my module?

Code:

var module = (function () {

    var module = function (name) {

        this.getName= function() {
            return name;
        }

    };

    module.prototype = {

        something: function () {
            // my function needs to access getName from here...
        }
    };

    return module;
})();

Usage:

var foo = module('nameValue');
foo.something();
Community
  • 1
  • 1
shenku
  • 11,969
  • 12
  • 64
  • 118

2 Answers2

0

Are you sure using apply rather than bind in this case wouldn't be a better implementation?

If you just want to make the class properties available for access in a modular setup, you'll need to expose them in the class function declaration. Then they'll be available for public access using those methods.

var module = (function () {

    function module (name) {

        // public method exposing *name* variable with "privileged" access
        this.getName= function() {
            return name;
        }

        // publicly exposing *name* variable itself (ahh! It's naked!)
        // this.name = name; // use only for read+write access to variable

    };

    // only setting one prototype parameter here so let's save some lines...
    module.prototype.something =  function () {

            return this.getName(); // or
            // return this.name // for direct variable exposure

    };

    return module;
})();

Then you can create your instances:

var mod1 = new module("bar");
var mod2 = new module("foo");
var mod3 = new module("win");

And apply a bind later on...

Though by using apply, you can do this:

var getNameOfModule = function(){ return this.getName(); }
getNameOfModule.apply(mod1); // bar
getNameOfModule.apply(mod2); // foo
getNameOfModule.apply(mod3); // win


This would depend entirely on the structure of your setup.


Also, it's good practice to start class names with a capital (as in Module vs. module).

strakez
  • 35
  • 5
-1

You may want to create first an anonymous function and then set the others, and then call getName() using a self reference to the module object.

var module = function () {

    var module = function(){};

    module.getName = function (name) {
        return name;
    };

    var self = module;

    module.prototype.something = function () {
        alert(self.getName('myName'));
    };

    return module;
};

var myMod = module();
myMod.prototype.something();

Here's the live example http://jsfiddle.net/ybfjB/

ricardohdz
  • 579
  • 4
  • 9
  • Do you think that myMod.prototype.something(); is a good way to write javascript? – Khanh TO Apr 15 '13 at 02:53
  • @KhanhTo You don't need the `.prototype` part when calling, it was probably an accident (or unknown) – Ian Apr 15 '13 at 02:57
  • in the above code the usage of the name parameter is in the form of a constructor for the entire module, which yours doesnt support - Ill update with usage. – shenku Apr 15 '13 at 03:07