0

sorry - I know this is dead easy stuff, but I'm just trying to learn.

I'm trying to understand objects in Javascript, I'm just trying to get the obj.alertHello() to alert hello, but it's alerting 'undefined' why is this?? - I'm seriously going bald over this!

Thanks any help is most appreciated!! :)

var obj=function(){    
this.sayHello="hello";

};

obj.prototype={
    alertHello: function(sayHello){
    alert(sayHello)
    }
}

1 Answer 1

1

sayHello and this.sayHello are different variables. You want something more like this:

var obj=function(){    
    this.sayHello="hello";
};

obj.prototype={
    alertHello: function(sayHello){
        alert(this.sayHello);
    }
};

var instance = new obj();
instance.alertHello();
​
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thank you Quentin! So, I can refer to any variable in the obj function at the top (is that what they call the constructor function??) by using the 'this' keyword, because it's a prototype right?? Thanks for every ones help on this!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.