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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:33:56+00:00 2026-06-10T01:33:56+00:00

Say I have the following javascript: function Foo() { function privateStuff() { //do private

  • 0

Say I have the following javascript:

function Foo() {

    function privateStuff() {
       //do private stuff
    }

    this.publicStuff = function() {
        privateStuff();
        //do other stuff
    }

}

It’s easy enough to test publicStuff() by doing the following:

var myFoo = new Foo();
myFoo.publicStuff();
//all the assertions

However, I want to be able to test the privateStuff() method as its own unit. I’m not sure how I could go about calling it on it’s own. I know with Java (with which I am much more familiar) you can use reflection to test private methods, but I’m wondering if there’s any way to test these functions on their own. If there’s no general way of doing this, I’ve started playing around with Jasmine to run my unit tests. Does this framework provide any capability for this?

  • 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-10T01:33:58+00:00Added an answer on June 10, 2026 at 1:33 am

    The way you have your code up there, you can’t. You don’t allow anything to have scoped access to privateStuff. It seems to me that you need to read up on the concept of closures (which is fundamental to JavaScript).

    Whenever you use a f() { ... } construct in JS (and several others, like try-catch blocks), you always implicitly create a closure. Nesting functions as you did is perfectly legal, but unless you give the external function an external reference to the internal function, the internal function can only be accessed from within the external function.

    From Mozilla:

    You can nest a function within a function. The nested (inner) function is private to its containing (outer) function. It also forms a closure.

    A closure is an expression (typically a function) that can have free
    variables together with an environment that binds those variables
    (that “closes” the expression).

    Since a nested function is a closure, this means that a nested
    function can “inherit” the arguments and variables of its containing
    function. In other words, the inner function contains the scope of the
    outer function.

    • The inner function can be accessed only from statements in the outer function.
    • The inner function forms a closure: the inner function can use the arguments and

    Even though the Mozilla documentation perpetuates this, it’s slightly incorrect to say stuff is private or public in Javascript as the nomenclature has very well-defined meaning in most programming languages (like C++ and Java) that relates to polymorphic behavior or inheritance. As far as JS is concerned, it’s better to think of it as scope-limited and try to gain a strong understanding of closures.

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

Sidebar

Related Questions

Let's say I have the following javascript: (function($) { $.getJSON(url, function(data) { $.each(data.rows, function(index,
Let's say for example, I have the following javascript function that returns a boolean:
Let's say we have the following JavaScript/jQuery code below function createElement(i, value) { return
I have this following code on my site : <script type=text/javascript> $(document).ready(function(){ $('.notif-email >
Lets say I have the following array in JavaScript: var skins = new Array('Light',
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
say I have the following <h3>This is my heading</h3> <p>This is my headingAnd this
Say I have the following chef role: name test description role for test run_list
Say I have the following DOM element in my piece of code: <div id=test>

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.