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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:56:09+00:00 2026-05-29T19:56:09+00:00

I have a JS function with the structure of function myFunc (method){ this.type =

  • 0

I have a JS function with the structure of

 function myFunc (method){
      this.type = method;
      this.border = '20';
      this.add = function(){
           // some codes
           privateFunc();
      }

      var privateFunc = function(){
           //my private function
      }
  }

The problem is, that the private function does not have access to any of the variables (this.type, this.border). They are not defined inside it! Why not?! How can I have a private function there with access to the variables?

  • 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-29T19:56:12+00:00Added an answer on May 29, 2026 at 7:56 pm

    Just for the sake of completeness

     function myFunc (method){ 
          this.type = method; 
          this.border = '20'; 
          // take a reference to the current object
          var self = this;
          this.add = function(){ 
               // some codes 
               privateFunc(); 
          } 
    
          var privateFunc = function(){ 
    
               //now you can access your members via self variable
               self.border = 10;
          } 
      } 
    

    The clue here is the usage of closure.

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

Sidebar

Related Questions

I have the following function : var appendStructure = { init : function(wrapper,structure,cls,callback) {
I have a function to traverse a complex deep HashMap structure. My problem is
I have seen a function whose prototype is: int myfunc(void** ppt) This function is
I have this recursion loop where inside the function I have atleast 2 ajax
I have a plugin with the following structure: (function($) { $.fn.drawFieldsTable = function(options) {
I have a function which gives me the complete file structure upto n-level, function
I am writing a function that takes a OpenCV Mat structure and does some
In a Django template, I have a function function show_graph(i){ $(#+i+seegraph).click(function(){ $(#+i+graph).show(); var qaa
Stupid problem, I have a function that runs a query several times in a
in my webapp I have a variable like this var data = { getItem:

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.