using prototype to construct a function

JZeig
JZeig used Ask the Experts™
on
Can someone till me what the problem is here?
function FileName(n, d, s) {
      this.name = n;
      this.date = d;
      this.service = s;
}
this.prototype.logInfo = function(){
      console.log(this.name);
};
var m0140415 = new FileName("m0140415", 20140415, "morning");
FileName.logInfo();

// as a side not, I've tried FileName.prototype.loginfo = function()
When I purse in Firefox, it indicate undefined function, and Aptana says I need a  semi; therefore, I added the semi to end of declared function.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
There's a number of ways to do this.

The 2 problems (first one you already tried the correct way):
1) Using the prototype on 'this' doesn't work in this context because you're setting it outside the function.
2) Calling the logInfo function is done on the created (instantiated) object, not on the function object.

This works:
function FileName(n, d, s) {
      this.name = n;
      this.date = d;
      this.service = s;
}
FileName.prototype.logInfo = function(){
      console.log(this.name);
};
var m0140415 = new FileName("m0140415", 20140415, "morning");
m0140415.logInfo();

Open in new window

Optionally try it like this:
var FileName = function (n, d, s) {
      this.name = n;
      this.date = d;
      this.service = s;
};

FileName.prototype.logInfo = function(){
      console.log(this.name);
};

var m0140415 = new FileName("m0140415", 20140415, "morning");
m0140415.logInfo();

Open in new window

or like this:
var FileName = function (n, d, s) {
      this.name = n;
      this.date = d;
      this.service = s;
      this.logInfo = function(){
            console.log(this.name);
      };
};
var m0140415 = new FileName("m0140415", 20140415, "morning");
m0140415.logInfo();

Open in new window

Author

Commented:
I appreciate the additional methods that you posted. Thank you very much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial