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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:31:56+00:00 2026-05-26T05:31:56+00:00

If you have an instance of an object in javascript, it seems it that

  • 0

If you have an instance of an object in javascript, it seems it that can be difficult to find its actual type, ie

var Point2D = function Point2D(x, y) {
  return {
    X: x,
    Y: y
  }
}

var p = new Point2D(1,1);

typeof p // yields just 'Object' not 'Point2D'

One way around it I found was to make the object its own prototype, and then you can gets its name effectively by calling prototype.constructor.name,

var Point2D = function Point2D(x, y) {
  return {
    X: x,
    Y: y,
    prototype: this
  }
}

new Point2D(1,1).prototype.constructor.name // yields 'Point2D'

Would this be an OK way of doing it (what are the pros/cons?) or is there a better practice I am missing out on?

Thanks.

  • 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-26T05:31:56+00:00Added an answer on May 26, 2026 at 5:31 am

    First we need to fix how you are building your class, since you are tripping in some JS pitfalls and doing some really weird stuff:

    function Point2D(x, y){
        //Our constructor/initialization function
        //when run, the 'this object will have
        //Point2D.prototype as its prototype
    
        this.x = x;
        this.y = y;
    
        //Don't return a value here! Doing so overrides
        //the default "return this" that we actually want.
    }
    
    //You can put things in the Point2D prototype in order to have them
    //be shared by all Point2D objects. Normally you want the methods to be shared.
    Point2D.prototype.getX = function(){
        return this.x;
    };
    
    //Note that there is nothing magical about the "prototype" property.
    //It is just where the `new` syntax expects the prototype it will use to be.
    //The actual magic property is __proto__ (don't depend on it though
    // __proto__ is browser specific and unsafe/evil)
    
    //We now can create points!
    var p = new Point2D(17.0, 42.0);
    p.getX();
    

    Now we can tackle the part about getting the type names. The better practice you are missing on is not inspecting the type in the first place. From an OO perspective it shouldn’t matter how an object is implemented (ts class) but how it behaves and what is its interface (exposed methods and properties). Also, from a Javascript perspective, type names are a second-class hack that don’t fit very well with the prototypical OO scheme that is actually used.

    Since there are many reasons you could be trying to inspect a type name in the first place, there are many different “best” solutions. Some that I could think of:

    1. If all you case about is “does this object implement a particular point interface” then you can do feature-inspection directly:

      function isPoint(p){
          //check if p has all the methods I expect a point to have:
          //(note that functions are truthy in a boolean context)
          return (p.getX && p.getY);
      }
      
    2. If you use the type names to do dispatching consider using a method instead. Its the natural OO way.

      function speak(obj){
          //fake pseudo-syntax:
          if(obj is of type Cat){ console.log("meow"); }
          if(obj is of type Dog){ console.log("woof"); }
      }
      

      becomes

      Cat.prototype.speak = function(){ console.log("meow"); };
      Dog.prototype.speak = function(){ console.log("woof"); };
      
    3. If you really need some sort of tag, you can explicitely make one, as pointed by some of the other answers already:

      Point2D.prototype.pointType = "2D";
      

      The advantages here are that you can have more than one class have the same “type” if you need to and that you don’t have to worry about any of the tricky instanceof or typeof corner cases.

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

Sidebar

Related Questions

I have this inside the HTML that is creating an object in JavaScript var
When you have an object instance in C#, you can use the this keyword
I have an object, that is facing a particular direction with (for instance) a
I have some object that is instantiated in code behind, for instance, the XAML
Example: I have an class that inherits from UIImageView. An object creates an instance
In C#, suppose you have an object (say, myObject ) that is an instance
I have a situation, where I need to create a new JavaScript object that
I have an instance function in javascript and for naming conventions I have other
I have a javascript function (class) that takes a function reference as one paremter.
I have a javascript object with several properties. The object also contains an instance

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.