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

  • Home
  • SEARCH
  • 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 994139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:32:00+00:00 2026-05-16T06:32:00+00:00

When the myClass function returns a single string hii, testClass.getDetails() works fine: function myClass(name,

  • 0

When the myClass function returns a single string “hii”, testClass.getDetails() works fine:

function myClass(name, age) {
    this.name = name;
    this.age = age;

    return "hii";
}
myClass.prototype.getDetails = function() {
    return "mydetails";
}

var testClass = new myClass('aneesh', 27);
alert(testClass.getDetails());

But when I return an object in myClass:

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

I get an error:

testClass.getDetails is not a function

Why does that happen? In Javascript a string is also an object, right?

  • 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-16T06:32:00+00:00Added an answer on May 16, 2026 at 6:32 am

    No, a string literal like the one you are returning ("hii") is a primitive value is not an object.

    In JavaScript we have the following primitives: string, number, boolean, undefined and null.

    If a constructor used with the new operator returns a primitive, the this value will be returned.

    If an object is returned, like in your second example (which IMO is not really useful), the newly created object (this within the constructor) will be lost, and you get an error because it doesn’t contain a property named getDetails.

    For example:

    function Test () {
      // don't return anything (equivalent to returning undefined)
    }
    
    new Test() instanceof Test; // true
    
    function Test2 () {
      return {};
    }
    
    new Test2 instanceof Test2; // false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function MyClass(projectName) { this.projectName = projectName; } MyClass.prototype.createHttpRequestObject = function() { /* do something
I need some help, I have created this function and it works fine, but
If I have a function that returns an object, but this return value is
I have a class, looks something like this: function MyClass() { this.array1 = [];
I have this static callback function in MyClass, and I try to call another
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I want to mplement function that returns enum: class myClass{ private: res _res; public:
class MyClass { function __constructor($A,$B,$C) { echo($A $B $C); } } $MC=new MyClass('Hello','World','!'); My
I'm having trouble with the MyClass::function(); style of calling methods and can't figure out

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.