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 306027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:25:42+00:00 2026-05-12T07:25:42+00:00

I know there are several other posts on this topic but they still leave

  • 0

I know there are several other posts on this topic but they still leave me confused.

I’ve included jQuery and everything and,
I have a simple javascript class like this example:

function CarConstructor(){
  this.speed=19; // in mph
  this.make="Ford";
  this.fillKph=fillKph;
}

function fillKph(){
  $("#kphdiv").html(this.speed*1.61);
}

car1 = new CarConstructor();
car1.fillKph();

Now I know that that code snippet doesn’t work and is not properly consturcted.

The “this” keyword there is referencing my dom element with the id of “kphdiv”.

The question I have is what is the best way to handle this.

Ive seen one method where you set some variable equal to this (binding it) and then use that variable to reference your object. For example:

function CarConstructor(){
  this.speed=19; // in mph
  this.make="Ford";
  this.fillKph=fillKph;
}

function fillKph(){
  var me=this;
  $("#kphdiv").html(me.speed*1.61);
}

car1 = new CarConstructor();
car1.fillKph();

I could also make the me a global variable … I don’t know.

I was just curious if there is another/better way.

  • 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-05-12T07:25:42+00:00Added an answer on May 12, 2026 at 7:25 am

    Oh boy, you are confusing quite a few things.

    function CarConstructor(){
      this.speed=19; // in mph
      this.make="Ford";
      this.fillKph; // <-> This particular statement has no meaning. 
      //When you write this.fillKph without any assignment, it will be 'undefined'. 
      //Just because you have a function named 'fillKph' somewhere else, 
      //it doesn't mean it will get attached to this property.
    }
    

    Try,

    var toyota = new Car();
    alert(typeof toyota.fillKph); //will alert undefined.
    

    The fillKph function is created in global scope, i.e. as property of ‘Window’ object.

    function fillKph(){
      var me=this;
      $("#kphdiv").html(me.speed*1.61);
    }
    

    To fix it, you can what rezzif suggested. Your final code will look like

    function Car()
    {
      this.speed=19; // in mph
      this.make="Ford";
      this.fillKph = function (){
          $("#kphdiv").html(this.speed*1.61);
      };
    }
    
    car1 = new Car();
    car1.fillKph();
    

    If you notice, I did not store reference to ‘this’ inside a local variable. Why? There is no need in this scenario. To understand more, see my detailed answer here.

    If you are going to create lot of Car objects, you can define the fillKph method on the prototype.

    function Car()
    {
      this.speed=19; // in mph
      this.make="Ford";
    }
    
    Car.prototype.fillKph = function fillKph() { $("#kphdiv").html(this.speed*1.61); };
    
    car1 = new Car();
    car1.fillKph();
    

    EDIT:

    If you do something like,

    function CarConstructor(){
      this.speed=19; // in mph
      this.make="Ford";
      this.fillKph = fillKph;
    }
    
    function fillKph(){
      $("#kphdiv").html(me.speed*1.61);
    }
    
    car1 = new Car();
    car1.fillKph(); //This will work as expected.
    

    But the problem is that fillKph is defined in ‘Window’ scope, so I can directly call it like,

    fillKph(); //Calling it this way will break it as it won't get correct 'this'.
    

    Point is,

    alert(typeof fillKph); // alerts 'function' if you do it your way,
    alert(typeof fillKph); // alerts 'undefined', if you do it the way I suggested, which is preferred in my opinion.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are several threads and posts regarding this issue in the internet
I know there have been a few threads on this before, but I have
I know there are several plugins that do asynchronous processing. Which one is the
I know there are several ways to deploy a .net windows client application: There's
I know it is easy to recommend several cross platform libraries. However, are there
I know there is a registry key indicating the install directory, but I don't
I know there are a lot of positive things mod-rewrite accomplishes. But are there
I have found several topics with this title, but none of their solutions worked
I know that there are several ways to define a function in JavaScript. Two
I have searched Google and Stackoverflow for this question, but I still don't understand

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.