1
var func_obj = function() {
    console.log('wat');
    this.my_proto_method();
};

func_obj.prototype.my_proto_method = function() {
    console.log('how do I programming');
};

​func_obj();​

I'm trying to get the above code to work. From what I've read this should work, not sure what I'm doing wrong here. Also setup a fiddle here

2 Answers 2

2

To access prototype object/method with this you have to create new instance of func_obj. If you want to access prototype methods withod instance then you have to use prototype property as func_obj.prototype.my_proto_method().

var func_obj = function() {
    console.log('wat');
    this.my_proto_method();
   // if called without new then access prototype as : func_obj.prototype.my_proto_method()
};

func_obj.prototype.my_proto_method = function() {
    console.log('how do I programming');
};

​new func_obj();​
Sign up to request clarification or add additional context in comments.

Comments

2

You need to prefix your call to func_obj with the new prefix:

var func_obj = function() {
    console.log('wat');
    this.my_proto_method();
};

func_obj.prototype.my_proto_method = function() {
    console.log('how do I programming');
};

​var foo = new func_obj();​

1 Comment

Thanks, I marked the other guys answer correct because of his answer about invoking without the new instance.

Your Answer

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