1
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?

4
  • You're not using the age argument anywhere. Your function simply checks if rob is older than sam, every single time. Nor is isOlderThan a method of sam. Commented Nov 23, 2018 at 14:37
  • people has no .isOlderThan() method. Why do you think sam.isOlderThan(rob) should work? (and constructor functions, functions that will be used with the new keyword, should start with a capital letter) Commented Nov 23, 2018 at 14:37
  • You still have to call the function. When I use isOlderThan(), i get back 'true', since rob is older than sam. Commented Nov 23, 2018 at 14:37
  • HI there, we generally need a little more info than 'it's not working' to help. What's the error, at what line does it occur etc. It makes it easier for us to hep you with more information! Commented Nov 23, 2018 at 14:37

5 Answers 5

2

//  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 ) );

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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.

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.