Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:18:25+00:00 2026-06-13T06:18:25+00:00

So let’s say you have a really basic person object with two values and

  • 0

So let’s say you have a really basic person object with two values and one function:

function personObject() {
    this.name = 'First Name';
    this.placeInLine = 1;
    this.setPlaceInLine = function(place) {
        this.placeInLine = place;
    }
}

And we setup some variables like this:

var john = new personObject();
var bill = new personObject();
var message = "";

Now look at the three codes snippets below…

—Code #1—

if(john.placeInLine < bill.placeInLine) message = "John is before Bill";
else message = "John is not before Bill";

RESULT: message = “John is not before Bill”; // because 1 is not less than 1

—Code #2—

bill.setPlaceInLine(2); // change Bill's place to 2 (instead of default of 1)
if(john.placeInLine < bill.placeInLine) message = "John is before Bill";
else message = "John is not before Bill";

RESULT: message = “John is before Bill”; // because 1 less than 2;

—Code #3—

if(john.placeInLine < bill.setPlaceInLine(2)) message = "John is before Bill";
else message = "John is not before Bill";

RESULT: message = “John is not before Bill”: // why?

Is the .setPlaceInLine function being called after the comparison? Or is the act of running that function returning something that is then being compared to john.placeInLine?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:18:26+00:00Added an answer on June 13, 2026 at 6:18 am

    Because setPlaceInLine method has no explicit return, and therefore returns undefined. And 1 < undefined evaluates to false: undefined gets converted to Number, giving NaN, and 1 < NaN is certainly false (1 > NaN is false too, btw).

    While you can fix this by making your setter method return the assigned value:

    PersonObject.prototype.setPlaceInLine = function(place) {
      return this.placeInLine = place;
    }
    

    … I think it’s better (more clean) to use the setters and getters separately (like in your code #2 example).

    As a sidenote, I’d recommend using prototypes to set up object methods (like I did in my example code). The reason for this is pretty well explained in this answer: basically with prototypes you will make just a single Function entity, used by all the created objects, when with this.someMethod you would create a new Function each time a constructor is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have two text files that I need to extract data out
Let's say I have the following data frame: > myvec name order_no 1 Amy
Let's say we have two pages in Orchard CMS. The homepage and the About
Let's say I have four samples: id=1, 2, 3, and 4, with one or
Let's say I have this element for displaying the website logo: <div id="web-title"> <a
Let's consider this code: (function(){ var a = {id: 1, name: mike, lastname: ross};
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.