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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:20:57+00:00 2026-05-20T10:20:57+00:00

Here is a little Javascript code snippet where I have used a function to

  • 0

Here is a little Javascript code snippet where I have used a function to simulate an object. I tried to reflect upon the member functions (which are really nested functions of the function) , but somehow the code does not work.

Can someone please help me understand why the code does not work. I am trying to understand the underlying principles of Javascript which cause this code to not work.

Thanks.

var test = function () {

    var first = function first () {
        alert ("first");
    }

    var second = function second () {
        alert ("second");
    }

};

function getOwnFunctions(obj) {
    for(var f in obj) {
        if(typeof(f) == "function" && obj.hasOwnProperty(f)) {
           alert(f);
        }
    }
}

getOwnFunctions(test);
  • 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-20T10:20:58+00:00Added an answer on May 20, 2026 at 10:20 am

    A couple points here:

    1. var defines the scope of the variable, which is saying that the
      first & second functions are
      only available inside the test
      function. To simulate a object,
      you want to use the this keyword.
    2. for(var f in obj): the for loops
      over the object and returns the
      keys in obj, so typeof(f) will always return the type of the
      f variable which will be a
      string, you want to check for
      typeof(obj[f]]) which will return
      the type of the actual underlying
      property.
    3. You want to call getOwnFunctions
      with a instance of test, not the
      actual test function:

    .

    var test = function () {    
        this.first = function() {
            alert ("first");
        }    
        this.second = function() {
            alert ("second");
        }    
    };    
    function getOwnFunctions(obj) {
        for(var f in obj) {
            if(typeof(obj[f]) == "function" && obj.hasOwnProperty(f)) {
               alert(f);
            }
        }
    }
    
    getOwnFunctions(new test);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little bit of Javascript that almost works correctly. Here's the code:
I have a little problem with one of my javascript code. Here is the
I have a little problem with javascript form submit issue, here is the script
Just a little confused here... I have a function in postgres, and when I'm
I have the following JavaScript code which is inside a function which I am
I have a little javascript app set up here: http://jsfiddle.net/faYMH/ What i want it
I am having a little problem here. I have a <script language =JavaScript runat=server>
So I have this neat little javascript function that I'm using to print text
I'm facing a little problem with my jQuery / Javascript code. I have several
I have javascript code like below - function initPage(){ // Left Navigation Pane -

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.