Tony Merryfield
Tony Merryfield

Reputation: 403

Using the this keyword in my namespaced JavaScript

I'm creating a small library of oft-used functions and want to refer to them within my namespace using the this keyword - searching has produced many different conflicting suggestions, tips and syntax. My structure looks like this:

var LibraryName = {

    someValue: 0,

    firstFunc: function(stuff) {

        LibraryName.someValue = 99;
        LibraryName.secondFunction();

    },

    secondFunction: function() {

        LibraryName.doSomething(LibraryName.someValue);

    }

}

So what I'm wondering is, do I have to refer to the full function name inside my namespace or can I use the keyword 'this' - I tried:

var LibraryName = {

    someValue: 0,

    firstFunc: function(stuff) {

        this.someValue = 99;
        this.secondFunction();

    },

    secondFunction: function() {

        this.doSomething(this.someValue);

    }

}

But I got errors stating that:

Uncaught TypeError: undefined is not a function

Thanks in advance!

Upvotes: 0

Views: 164

Answers (4)

Suchit kumar
Suchit kumar

Reputation: 11869

you can try like this:

 var LibraryName = {

                someValue: 0,

                firstFunc: function(stuff) {

                    this.someValue = 99;
                    this.secondFunction();

                },

                secondFunction: function() {

                    this.doSomething(this.someValue);

                },
                doSomething: function(value){
                    alert(value);
  }

            }

LibraryName.firstFunc();// since the at the creation time of `object LibraryName` ,properties and methods are added we can directly call them using `.` operator.

Upvotes: 0

DanielST
DanielST

Reputation: 14163

I prefer this method:

http://jsfiddle.net/71mLyk28/1/

var LibraryName = function () {
    //constructor stuff
}

LibraryName.prototype = {
    someValue: 0,
    firstFunc: function (stuff) {
        this.someValue = 99;
        this.secondFunction();
    },
    secondFunction: function () {
        this.doSomething(this.someValue);
    },
    doSomething: function(){
        console.log("success");
    }
}

var l = new LibraryName(); // instantiate a LibraryName object
l.firstFunc();

Adding to the prototype instead of directly to the object helps with memory usage (if you have more than one of this object) and with inheritance.

See JS - Why use Prototype?

Upvotes: 2

Arber Sylejmani
Arber Sylejmani

Reputation: 2108

That's because this.doSomething isn't defined anywhere, so if you replace it with this.firstFunc you won't have that Error

Upvotes: 0

Amir Popovich
Amir Popovich

Reputation: 29846

The doSomething method doesn't exist in your object:

 var LibraryName = {   
      someValue: 0,    
      firstFunc: function (stuff) {
          this.someValue = 99;
          this.secondFunction();  
      },   
      secondFunction: function () {
          this.doSomething(this.someValue);
      },  
      doSomething: function (val) {
          console.log('dosomething');
      }
  };

P.s. using this can be tricky if you reference your methods. Read here.

JSFIDDLE.

Upvotes: 1

Related Questions