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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:37+00:00 2026-06-06T19:20:37+00:00

EDIT: I figured it out from Bergi’s answer in the end. Thanks Bergi. pubPrivExample

  • 0

EDIT: I figured it out from Bergi’s answer in the end.

Thanks Bergi.

pubPrivExample = (function () {
    return {
        init : function () {
            var private;

            this.setPrivate = function (p) {
                private = p;
            };
            this.getPrivate = function () {
                return private;
            };
        },

        public : "This is public\n"
    };
}());

var a;

a = Object.create(pubPrivExample);
a.init();
a.setPrivate("This is private");

document.write(a.getPrivate());

EDIT: It seems the answers to my question are off at a tangent. I’m really not interested in a factory and actually would rather not use if. My question is about private state. From Bergi’s answers and comments I think I can pull something together.

To be continued…

EDIT: Bergi has started to answer the question below, but left out the most important part – the private state.

I have had time to think about the idea more, but am still unable to achieve private state using Object.create() without some kind of factory. But I want to be wrong, and Bergi alluded to a solution… Feel free to take Bergi’s answer as a starting point.

ORIGINAL: My quest to avoid new in javascript has lead me to a peculiar place. I want private object members, but I don’t want to give up Object.create().

Here’s the code.

var trackQueue = {};

trackQueue.factory = function () {
    var that, queue;
    that = this;
    queue = [];

    that.push = function (item) {
        queue.push(item);
    };

    that.work = function () {
        document.write(queue + "<br />");
    };

    return {
        work : that.work,
        push : that.push
    };        
};

var a = Object.create( trackQueue.factory() );
a.push("a");
a.push("b");
a.push("c");

var b = Object.create( trackQueue.factory() );
b.push("d");
b.push("e");
b.push("f");

a.work();
b.work();

And a jsfiddle

http://jsfiddle.net/dsjbirch/Wj6cp/10/

Would init be a more idiomatic / appropriate name for the factory method?

Is this insane?

Be kind – javascript isn’t my first language.

  • 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-06T19:20:38+00:00Added an answer on June 6, 2026 at 7:20 pm

    Yes, a init method on the prototype might be a more appropriate name:

    var proto = {
        init: function(args) {
            // setting up private-scoped vars,
            var example = args;
            // privileged methods
            this.accessPrivate = function(){ return example; };
            // and other stuff
            this.public = 5;
        },
        prop: "defaultvalue",
        ...
    }
    
    var instance = Object.create(proto);
    instance.init();
    

    However, there is absolutely no reason not to use the classical constructor with the new keyword, which elegantly combines the Object.create and init call.

    And note that you are using Object.create with absolutely no use. Your factory pattern (perfectly valid applied) returns good objects. No need to create new objects for each one that inherit from them. Just do:

    var instance = trackQueue.factory();
    

    If you like the sound of the method name “create”, you might use a more idiomatic name for your factory:

    trackQueueFactory.create = function(args) {...};
    

    EDIT: Your idea to combine the factory pattern with prototype inheritance is not so wrong. Yet, the proto object from which all the fabricated objects inherit needs to be static, instead of creating a new one on each invocation. Your code might look like this:

    var factory = {
        proto: {
            ...
        },
        create: function(args) {
            var product = Object.create(this.proto);
            // set up private vars scoped to the create function
            // privileged methods
            product.doSomethingSpecial = function(){ ... };
            // and other stuff
        }
    };
    
    var a = factory.create(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I figured out the answer to the original YUI3 question I posted here,
EDIT SO I figured out a fix. I replaced the while statement with an
Edit: I think I've figured out how to do the binary data part. Double
EDIT AGAIN ... I'm just a dummy and figured it out! EDIT: So, it
EDIT: I figured out how to solve the specific code broken part of my
Edit: I figured this out. jQuery Cycle plugin adds a z-index to every item
EDIT: Based on one of the posts below, i figured out how to write
EDIT: Figured out the problem. Also if you found this through google or another
REVISED: Okay, thanks to all of your input, I figured out what I was
-Edit: Solution Found- Figured it out after some heavy searching - one person (I

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.