crumbleweed992
crumbleweed992

Reputation: 11

Function coming back undefined in Javascript

function people (name, age) {
  this.name = name;
  this.age = age;
}

var rob = new people("robert jr", 41);
var sam = new people("sam davies", 25);

function isOlderThan(age) {
  if(rob.age > sam.age) 
    return true;
  else return false;
}

I tried running it with this sam.isOlderThan(rob);

But it's not working. I'm kinda new to this, any help?

Upvotes: 1

Views: 267

Answers (5)

The fourth bird
The fourth bird

Reputation: 163207

The function isOlderThan is not defined on people.

You could use a prototype on people if you want to call it like sam.isOlderThan(rob);

function People(name, age) {
  this.name = name;
  this.age = age;
}

var rob = new People("robert jr", 41);
var sam = new People("sam davies", 25);

People.prototype.isOlderThan = function(p) {
  return this.age > p.age;
};

console.log(sam.isOlderThan(rob));
console.log(rob.isOlderThan(sam));

Upvotes: 1

Shushan
Shushan

Reputation: 1225

Hi and welcome to StackOverflow. Assuming you mean to use the people function to define a type of class, and isOlderThan as a type of function of that class - notice the changes.

  1. Constuctor functions (such as People) are commonly written with a leading Capital letter
  2. For linking isOlderThan to an existing class - you should use the prototype syntax as shown below.
  3. Inside the isOlderThan function we make a reference to this - which indicates the current object for which the isOlderThan than function was called.

Now we can call the isOlderThan function for each People object we create.

function People(name, age) 
{
  this.name = name;
  this.age = age;
}

var rob = new People("robert jr", 41);
var sam = new People("sam davies", 25);


People.prototype.isOlderThan = function(age) {
    if (this.age > age) 
        return true;
     else return false;
}

console.log(sam.isOlderThan(50))
console.log(rob.isOlderThan(sam.age))

enter code here

Upvotes: 1

RobertFrenette
RobertFrenette

Reputation: 627

See if this Fiddle helps:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

var rob = new Person("robert jr", 41);
var sam = new Person("sam davies", 25);

function isOlderThan(rob, sam) {
  if (rob.age > sam.age) {
    return true;
  } else {
    return false;
  }
}

console.log(`Rob is older than Sam: ${isOlderThan(rob, sam)}`);

https://jsfiddle.net/ao2ucrmq/1/

Upvotes: 0

Shilly
Shilly

Reputation: 8589

//  If you want to be able to call this as sam.isOlderThan()
//  you need to use a prototype to add that function to the persons
//  so they all have that function bound to them.
function person (name, age) {
  this.name = name;
  this.age = age;
}
person.prototype.isOlderThan = function( other_person ) {
  return this.age > other_person.age;
};
var rob = new person("robert jr", 41);
var sam = new person("sam davies", 25);

console.log( sam.isOlderThan( rob ) );
console.log( rob.isOlderThan( sam ) );

Upvotes: 2

Jashwant
Jashwant

Reputation: 28995

You cannot use sam.isOlderThan because there is no isOlderThan function defined in people function ( correlate with class of other languages ).

isOlderThan is defined in global scope.

So, you can run it as isOlderThan() or window.isOlderThan() ( provided you are inside browser )

There's no need to pass age, rob or sam as they are defined in global scope and is available inside isOlderThan function.

Upvotes: 0

Related Questions