0

I'm trying to create an object in JavaScript and I'm following Mozilla's tutorial . the tutorial works just fine, but when I apply that technique to my code it doesn't work. (I'm doing something wrong but I don't see it). I coded all my methods and I don't get any errors, I initialize my object and I don't get any errors either, I even call my methods and I don't get errors, but the return value is a string with my code instead of the value that I'm expecting

function JavaScriptObj(id, datatype) {
    function initialize(id, datatype) {
        if (typeof id === 'number' && id > 1) {
            this.theID = id;
        } else {
            console.error("ERROR: JavaScriptObj.initialize" + id + "is NOT a valid argument");
        }

        if (typeof datatype === 'string') {
            this.data_type = datatype;
        } else {
            console.error("ERROR: JavaScriptObj.initialize" + datatype + "is NOT a valid argument");
        }
    }
}

JavaScriptObj.prototype.getSectionName = function(){
    var SectionName = "section-" + this.theID;
    return SectionName;
};
var person2 = new JavaScriptObj(2, "texteditor");
alert(person2.getSectionName);

this is my jsfiddle

thanks in advance! :-)

2
  • 4
    You need to call the function: alert(person2.getSectionName()); Also, console.log() is generally better for debugging than alert(). Commented Oct 26, 2013 at 22:57
  • I do not think that initialize is called explicitly as well. You could defined theID on the object as this.theID = id on line 3. Commented Oct 26, 2013 at 23:00

2 Answers 2

2

Remove the initialize nested function:

function JavaScriptObj(id, datatype) {
    if (typeof id === 'number' && id > 1) {
        this.theID = id;
    } else {
        console.error("ERROR: JavaScriptObj: " + id + "is NOT a valid argument");
    }

    if (typeof datatype === 'string') {
        this.data_type = datatype;
    } else {
        console.error("ERROR: JavaScriptObj: " + datatype + "is NOT a valid argument");
    }
}

JavaScriptObj.prototype.getSectionName = function(){
    var SectionName = "section-" + this.theID;
    return SectionName;
};

var person2 = new JavaScriptObj(2, "texteditor");
alert(person2.getSectionName()); // need to call it too
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're not actually executing/calling your method. In order to call your method, you need to append parenthesis to the call:

alert(person2.getSectionName());

Small aside -- using console.log() instead of alert() tends to save you a few keystrokes and makes development a bit faster. Also, alert() is a blocking call that stops all other code execution on the page. While that won't make a difference when you're first starting out, it could potentially be a pain point down the road as your javascript ninja skills increase. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.