2

http://jsfiddle.net/totszwai/WvbPn/2/

function DialogBox() {
    this.__DEBUGGING__ = false;

    DialogBox.debug = function (b) {
        this.__DEBUGGING__ = b;
    };

    DialogBox.test = function (b) {
        alert("hello worodl");
    };
};


$(document).ready(function () {
    dialogbox = new DialogBox();
    dialogbox.test();
});

I can't figure out what I did wrong in there. I tried it with

DialogBox.prototype.test
DialogBox.test
test

I am trying to make it so that when calling its own function internally, I don't need to put this all the time... example: this.test()

UPDATE: Also is there a way to not type "this" everywhere when calling private function? Normally I just write simple global function that is for one time use, but now I'm trying to write something different, and that I will be calling these private function all over the place within my class. So I am trying to just avoid using "this" everywhere... not to mention it makes the code readability pretty bad.

Like for example in Java (not JS), you don't need to type "this" everywhere.

codenamezero
  • 2,724
  • 29
  • 64
  • 1
    Also, the example you gave puts dialogbox into the global namespace. If this isn't by design, put "var" in front. – beatgammit Mar 16 '13 at 01:39

1 Answers1

4

For a quick fix:

Inside the DialogBox definition, use this. to define its methods:

http://jsfiddle.net/AaronBlenkush/WvbPn/4/

function DialogBox() {
    this.__DEBUGGING__ = false;

    this.debug = function (b) {
        this.__DEBUGGING__ = b;
    };

    this.test = function (b) {
        alert("hello worodl");
    };
};


$(document).ready(function () {
    dialogbox = new DialogBox();
    dialogbox.test();
});

For a comprehensive answer:

There's just too much to fit into a StackOverflow answer.

For a good read on this subject, see Addy Osmani's book Learning JavaScript Design Patterns, especially the part about the Constructor pattern, and surrounding sections.

Aaron Blenkush
  • 3,034
  • 2
  • 28
  • 54
  • 1
    Ahh... How about calling the function within debug()? Say for example, calling dialogbox.deubg(), and then test() inside debug. It kind of forces me to use this.test(), but is kind of annoying and the syntax is not clean... I've tried going different way to avoid using "this" inside the class.. ugh. – codenamezero Mar 16 '13 at 07:08
  • @codenamezero: **Using `this` inside the class constructor is the correct way.** I'm not sure what you mean by "the syntax is not clean". You could append a new section to your question (titled "EDIT" or "UPDATE" would be appropriate) if you want to elaborate :-) I know it's confusing -- are unfamiliar with OOP mechanisms like inheritance, polymorphism, encapsulation, etc? Just remember that everything in JavaScript is an **object** -- including **functions**, which is why we can use `this` to refer to *instances* of that function (which when used in this way we call it a **class**). – Aaron Blenkush Mar 16 '13 at 15:17
  • @codenamezero: Here's a useful answer to a StackOverflow question about the use of "this inside function": http://stackoverflow.com/a/1963390/873263 – Aaron Blenkush Mar 16 '13 at 15:18
  • I know OOP but take Java for example, you have private function inside a class, and when we call a function, we just type the function name without typing "this"... is just that I just found it weird that in JS class we have to put "this" everywhere when calling private functions. – codenamezero Mar 16 '13 at 21:54