Why can't I declare that the constructor instantiates the object and then accesses the prototype?

The below does not work and I am struggling to understand why ....

function CommentHandler() {

    var Text;
}


var myCommentHandler = new CommentHandler();

myCommentHandler.prototype.SayHello = function () {

    document.write('Hello World');

}

      

I am getting the error: 'myCommentHandler.prototype is undefined'

I just can't figure out why? I declared a variable called myCommentHandler and copied it into the CommentHandler object, after which Im trying to access the protoype and assign a function, but I can't ... Does anyone know why?

+1


source to share


5 answers


The prototype belongs to the class, not the instance:

CommentHandler.prototype.MyFunction = ...

      

However, you can also access the prototype using the instance property constructor

:

myObj.constructor.prototype.MyFunction = ...

      

This will of course add this function to every instance of your CommentHandler.



If, instead, you wanted to add this function to just one instance, you would do:

myObj.MyFunction = ...

      

Note that in none of these options is it possible to MyFunction

be able to access your private variable Text

.

This is a trade-off imposed by the prototype inheritance model - variables and methods must be public (for example this.Text

) if they need to be accessed from the outside, in which case the functions declared in the prototype are actually "outside".

The alternative (using closed-scoped variables and methods) seems more natural to OO programmers, but makes inheritance much more difficult.

+4


source


I think what you really want here:



CommentHandler.prototype.SayHello = function () {
    document.write('Hello World');
};

      

0


source


You must use

CommentHandler.prototype.SayHello = function(){....}

      

You can find more about object prototype in the following links
http://www.javascriptkit.com/javatutors/proto.shtml
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/prototype
http: // msdn.microsoft.com/en-us/magazine/cc163419.aspx

0


source


You need to prototype the class function, not the instance. Also, if you want the variable to Text

be available outside the constructor, you need to add the keyword in the other functions of the method this

.

function CommentHandler() {
    this.Text = 'Hello World';
}

CommentHandler.prototype.SayHello = function () {
    document.write(this.Text);
}

var myCommentHandler = new CommentHandler();
myCommentHandler.SayHello();

      

As a side item, you can create a function SayHello

inside the constructor, but this will add increased overhead to creating each instance CommentHandler

. Thus, the prototyping method you are using is the best option.

0


source


myCommentHandler

has no prototype because it is an instance. CommentHandler

has a prototype that can be changed. I assume you want to edit the object after initialization and can do it like this:

myCommentHandler.SayHello = function() {
    document.write('Hello World');
}

      

Here is a good explanation of prototypes and object oriented programming in javascript with many examples: http://mckoss.com/jscript/object.htm

0


source







All Articles