INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Methods with Object Constructors

Methods with Object Constructors

(OP)
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>

RE: Methods with Object Constructors

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.

RE: Methods with Object Constructors

(OP)
Thank you for your time but I am trying to practice using methods in constructors.

RE: Methods with Object Constructors

Quote:


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()) 

RE: Methods with Object Constructors

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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close