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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:53:50+00:00 2026-05-26T16:53:50+00:00

Is there a JavaScript pattern which mimics Protected object properties like what you see

  • 0

Is there a JavaScript pattern which mimics “Protected” object properties like what you see in languages like C++ ??

Basically, I’d like to create an Object A which has a number of “protected” object properties which can be accessed ONLY from methods which are defined from the prototype of Object A. i.e. – NOT accessible publicly from non-prototyped methods of A.

For instance, ideally would be like so:

function A(){
    var prop1 = 1;      
}

A.prototype.myFunc = function(){
    var newVar = this.prop1;   //newVar now is equivalent to 1
}

var instanceOfA = new A();
var newVar2 = instanceOfA.prop1;  //error given as prop1 is "protected"; hence undefined in this case

BTW – I do not want the pattern of privileged member functions accessing private properties since the member function is still public.

  • 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-26T16:53:51+00:00Added an answer on May 26, 2026 at 4:53 pm

    There is no object property that can only be accessed from prototyped methods of A and not from non-prototyped methods of A. The language doesn’t have that type of feature and I’m not aware of any work-around/hack to implement it.

    Using Doug Crockford’s methods, you can create member properties that can only be accessed from predefined non-prototyped methods (those defined in the constructor). So, if you’re trying to limit access only to a predefined set of methods, this will accomplish that. Other than that, I think you’re out of luck.

    If you want other ideas, you’d probably get more help if you describe more about what you’re actually trying to accomplish in your code rather than just how to emulate a feature in another language. Javascript is so much different than C++ that it’s better to start from the needs of the problem rather than try to find an analogy to some C++ feature.

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

Sidebar

Related Questions

In javascript there's this sweet, sweet function window.setTimeout( func, 1000 ) ; which will
Is there a JavaScript library which allow me to perform all the REST operation
I have begun writing my 'class' type JavaScript functions like the Module Pattern or
I have a fairly complex object which has some C# code written to render
I have an object which looks like: {a: 1, b:2, c:3, cp1:6, cp2:7 cp3:8,
Are there methods by which I can skip a particular number of objects and
I am working on a project which has a lot of JavaScript. I think
Are there any commonly used patterns in Javascript for storing the URL's of endpoints
Is there any JavaScript method similar to the jQuery delay() or wait() (to delay
Is there a javascript function I can use to detect whether a specific silverlight

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.