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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:01:03+00:00 2026-05-31T00:01:03+00:00

I am working on trying to make an JS object and access to private

  • 0

I am working on trying to make an JS object and access to private methods. The problem I am running into when trying to return a function is the private methods cannot be accessed. Code Below.

var Item = (function() {

    var price = 0;
    var name = '';
    var description = '';
    var quantity = '';
    var attributes = {};

    var Item = function(data) {

    }

    function setPrice(variable) {
        this.price = variable;
    };

    function getPrice() {
        return this.price;
    };

    function setName(variable) {
        this.name = variable;
    };

    function getName() {
        return this.name;
    };

    function setDescription(variable) {
        this.description = variable;
    };

    function setQuantity(variable) {
        this.quanity = variable;
    };

    return function(data){

        setPrice : setPrice;
        getPrice : getPrice;
        setName : setName;
        setDescription : setDescription;
        setQuantity : setQuantity;

        return new Item(data);
    }

})();

item2 = Item();
    item2.setPrice('3');
alert(item2.getPrice());

With this setup, how can I access the private methods?

  • 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-31T00:01:05+00:00Added an answer on May 31, 2026 at 12:01 am

    I don’t think that pattern will work for what you’re trying to do. I think using a pattern like this one will keep your code smaller and more reusable. This way you also get rid of the set functions.

    var Item = function(options) {
    
        var opts = $.extend({
            price: 0,
            name: '',
            description: '',
            quantity: '',
            attributes: {}
        }, options);
    
        // ...
        this.getPrice = function() {
            return opts.price;
        };
    
        // ...
    };
    
    var item = new Item({
        price: 100,
        name: 'onehundred',
        // ...
    });
    
    alert(item.getPrice());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in VB.Net and I'm trying to make a piece of code more
I'm trying to make it working for quite some time,but just can't seem to
I'm working on some SharePoint web parts and I'm trying to make them as
Working on a new app and using restful-authentication. I was trying to make it
I'm working on an app in CodeIgniter, and I'm trying to make a field
I am working on a database schema, and am trying to make some decisions
I'm trying and trying and can't make it working. I have a ul class=first
So I'm working on a simple iPhone game and am trying to make a
I'm trying to make a function that can take an enum type, display all
I'm trying to make a dice object, and I want to be able to

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.