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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:46:44+00:00 2026-06-10T22:46:44+00:00

What I want to do is extract the name of a function in JavaScript.

  • 0

What I want to do is extract the name of a function in JavaScript. I got this working some time ago and it looks something like this:

MyObj = function myobj(){};

extend = function(obj){
    return /function (.+)\(/.exec(obj.constructor.toString())[1];
}

So here is the funny thing. When I use prototype with this object in this way, it all works fine:

MyObj.prototype.a = function(){}
MyObj.prototype.b = function(){}

extend(MyObj);
//->'myobj'

However, when I define my function like this:

MyObj.prototype = {
   a : function(){},
   b : function(){}
}

extend(MyObj);
//->'Object'

Does anybody has any idea why the constructor in the latter method is part of JavaScript’s native code (e.g. ‘Object’), instead of my function?

Any help would be greatly appreciated!

  • 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-10T22:46:46+00:00Added an answer on June 10, 2026 at 10:46 pm

    Try adding the constructor property in your second case.
    Because you’re overwriting the prototype, you’re also overwriting the obj.prototype.constructor property.
    So use it like this :

    MyObj.prototype = {
        constructor : MyObj,
        method1 : ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I'm trying to do something like this, enum Data_Property { NAME, DATETIME, TYPE, };
I want to extract the file name 13572_BranchInformationReport_2012-06-28.zip from the following text - 1:30,/icons/def13572_BranchInformationReport_2012-06-28.zip,13572_BranchInformationReport_2012-06-28.zip,0,184296,Jun
I want to extract just the file name portion of a path. My code
I want to extract the value between the apostrophes, for example from this string:
I want to extract some links from a XML document (links are in same
I have a pointer to functions like this. TTestEvent = function(): Boolean; procedure ExecuteTest(aTest:
Say I request parent/child/child/page-name in my browser. I want to extract the parent, children
I'm trying to extract functions and function headers from some source code files. Here's
I want an approach/code snippet to extract column names and the corresponding table name

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.