Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Methods with Object Constructors

Status
Not open for further replies.

SharePointPunk

Programmer
Sep 15, 2015
6
US
I am trying to write a simple method to add to my object constructor. All the standard property's work fine but I cant get the method to return anything.

I am not sure if it needs to be a prototype since its inside a constructor.
Thanks for your help!

I am running "console.log(HR.Info);" inside firebug and getting undefined also once it returned function() not sure why.


<script>

var Opening = function(Position, Salary, Title){

this.Position = Position;
this.Salary = Salary;
this.Title = Title;
this.Info = function(){
return "You will be working in"+ Position +"making" + Salary +"you will be called" + Title;
}
}

var HR = new Opening("IT", 110000, "Director");


</script>
 
Hi there,

your this.Info is still lacking a bit spacing and maybe a new line, but I got the correct return value like this:
Code:
var Opening = function (Position, Salary, Title) {

	this.Position = Position;
	this.Salary = Salary;
	this.Title = Title;
	this.Info = "You will be working in" + Position + "making" + Salary + "you will be called" + Title;
}
var HR = new Opening("IT", 110000, "Director");
alert(HR.Info);


"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thank you for your time but I am trying to practice using methods in constructors.
 
I am running "console.log(HR.Info);" inside firebug and getting undefined also once it returned function() not sure why.
If its a function you have to run it with () like
Code:
console.log(HR.Info[highlight]()[/highlight])
 
Example:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>SharePointPunk</title>
  </head>
  <body>
  <h1>SharePointPunk</h1>
  <script type="text/javascript">
  
  var Opening = function(Position, Salary, Title){
    this.Position = Position;
    this.Salary = Salary;
    this.Title = Title;
    this.info = function(){
      return "You will be working in "+ Position + ", making " 
      + Salary +", you will be called " + Title;
    }
  }
 
  var hr = new Opening("IT", 110000, "Director");
  console.log(hr);
  console.log(hr.info());    
  
  </script>
  </body>
</html>

Output on FireFox console:
Code:
Object { Position: "IT", Salary: 110000, Title: "Director", info: Opening/this.info() }
You will be working in IT, making 110000, you will be called Director
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top