SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    826
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    a little OOP question

    actually, it's not strictly an OOP question... prob is var inside fn inside nested fn returns 'undefined'...

    Code:
    function Employee() {
    	this.manager = "Ravi Gupta";
    	this.dept = "sales";
    	this.city = "San Francisco";
    	this.setVacationDays = function(years) { 
    		vacationDays = years * 5; 
    		console.log(vacationDays); //	***** prints 'undefined'
    		console.log('years: ' + years);  // prints '4' (i.e., this var is fine)
    	};	
    }
    	
    
    	var Jon = new Employee();
    	var JonVacationDays = Jon.setVacationDays(4);
    	console.log(JonVacationDays);	//	***** prints 'undefined'

    why is var JonVacationDays undefined??

    thank you....

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    41
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi maya90

    The reason JonVacationDays returns "undefined" is because you aren't returning any value in the "setVacationDays" function.
    Code:
    	this.setVacationDays = function(years) { 
    		var vacationDays = years * 5;
    		console.log(vacationDays); //	***** prints 'undefined'
    		console.log('years: ' + years);  // prints '4' (i.e., this var is fine)
    		return vacationDays; 
    	};
    If the only action done in "setVacationDays" is multiplying by 5, then you can just return that ("return years * 5;")

    Edit: if I print "console.log(vacationDays)" it does return 20.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    826
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    awesome... thank you.....

    you know, I took it from here,
    http://www.webreference.com/js/column80/3.html

    I guess this fn,

    Code:
     this.setRegisterA = function(param) { registerA = param };
    doesn't need "return" stmt.......;-)

    not sure sometimes, when you need that "return" stmt at the end & when you don't..... ;-)

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    41
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to add a return-statement to a function if you want the function to return a value. If you just want to execute a function, you don't need to write a return-statement.

    Eventough my "solution" works, it's not a decent way of writing. I'll try to clarify.
    - We're writing a class "Employee", so we should follow the OOP "standards" :-)
    - We've created a setVacationdays-function, but we're returning a value. Basically, when writing a "setter"-function, you never return a value. You either write a getter-function or just call upon the variable.
    - We haven't declared the vacationDays-variable

    So, lets rewrite it:
    Code:
    function Employee() {
    	this.manager = "Ravi Gupta";
    	this.dept = "sales";
    	this.city = "San Francisco";
    	this.vacationDays = 5;
    	this.setVacationDays = function(years) { 
    		this.vacationDays = years * 5; 
    	};
    		
    	this.getVacationDays = function(years) { 
    		return this.vacationDays; 
    	};
    }
    	
    var Jon = new Employee();
    Jon.setVacationDays(4);
    console.log(Jon.vacationDays);
    console.log(Jon.getVacationDays());
    So the code was slightly altered. In the first console.log, I call upon the variable using the new Employee, Jon, which was created earlier. In the second console.log, I call upon the getter-function. The only job the getter-function has is to return this.vacationDays.

    I hope it's a bit more clear now, it's not easy explaining it in English ;-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •