javascript - Why is my function being returned correctly when prototyped outside of my constructor, but not when it's being used as a method within my constructor? -


i have function constructor needs have method being used 1 of properties within constructor. normally, i'd create function property so:

var currentdate = new formatdate(new date()); console.log(currentdate.year, currentdate.month, currentdate.dayofmonth, currentdate.dayofweek, currentdate.second);   function formatdate(dateholder){     this.monthsarray = ["jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sep", "oct", "nov", "dec"];     this.daysofweekarray = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];     this.date = new date(dateholder);     this.year = this.date.getfullyear();     this.month = this.monthsarray[this.date.getmonth()];     this.dayofmonth = this.addzero(this.date.getdate());     this.dayofweek = this.daysofweekarray[this.date.getday()];     this.minute = this.addzero(this.date.getminutes());     this.second = this.addzero(this.date.getseconds());     this.addzero = function(i){         if(i < 10){             = "0" +i;         }         return i;     } } 

i've been taught work , way this. however, whenever run application, the error saying this.addzero not function. when same thing, prototype addzero method so

formatdate.prototype.addzero = function(i){     if(i < 10){         = "0" +i;     }     return i; }  function formatdate(dateholder){     this.monthsarray = ["jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sep", "oct", "nov", "dec"];     this.daysofweekarray = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];     this.date = new date(dateholder);     this.year = this.date.getfullyear();     this.month = this.monthsarray[this.date.getmonth()];     this.dayofmonth = this.addzero(this.date.getdate());     this.dayofweek = this.daysofweekarray[this.date.getday()];     this.minute = this.addzero(this.date.getminutes());     this.second = this.addzero(this.date.getseconds()); } 

it works way it's supposed to.

can explain me why it's working 1 way not other, if i'm doing wrong(and how fix it), , way right way?

you're calling addzero before you've defined it. switch around define first , work:

this.addzero = function(i){     if(i < 10){         = "0" +i;     }     return i; }; this.dayofmonth = this.addzero(this.date.getdate()); this.dayofweek = this.daysofweekarray[this.date.getday()]; this.minute = this.addzero(this.date.getminutes()); this.second = this.addzero(this.date.getseconds()); 

or, since function doesn't use this can use function declaration , rely on hoisting:

this.dayofmonth = addzero(this.date.getdate()); this.dayofweek = this.daysofweekarray[this.date.getday()]; this.minute = addzero(this.date.getminutes()); this.second = addzero(this.date.getseconds());  function addzero(i){     if(i < 10){         = "0" +i;     }     return i; }; 

arguably should use prototype whenever can anyway (when body of function doesn't need access variables declared in constructor). functions added prototype reused every object, functions created in constructor duplicated (a new function created each object, not necessary when don't use variables closed over).


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -