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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:01:08+00:00 2026-06-08T00:01:08+00:00

In JavaScript I see a few different ways, certain tasks can be performed within

  • 0

In JavaScript I see a few different ways, certain tasks can be performed within an object for example, the object Egg I have below.

Can anyone tell me the difference between each one, why I would use one and not the other etc

 var Egg = function(){

    //Properties

    var shell = "cracked" // private property 

    this.shell = "cracked" // public property

    shell: "cracked" // what is this??

    //functions

    function cook(){

        //standard function
    }

    cook: function(){
        //what kind of function is this?
    }

    //not sure what this is

    details: {
        //What is this? an array :S it holds 2 elements?
        cost: 1.23,
        make: 'Happy Egg';
    }




}
  • 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-08T00:01:09+00:00Added an answer on June 8, 2026 at 12:01 am

    Sure, Ben.

    This sort of gets to the bottom of the dynamism of JavaScript.
    First, we’ll look at basics — if you’re coming from a place where you understand class-based languages, like, say, Java or C++/C#, the one that is going to make the most sense is the constructor pattern which was included very early on:

    function Egg (type, radius, height, weight) {
        // private properties (can also have private functions)
        var cost = (type === "ostrich") ? 2.05 * weight : 0.35 * weight;
    
        // public properties
        this.type = type;
        this.radius = radius;
        this.height = height;
        this.weight = weight;
        this.cracked = false;
    
        // this is a public function which has access to private variables of the instance
        this.getCost = function () { return cost; };
    }
    
    // this is a method which ALL eggs inherit, which can manipulate "this" properly
    // but it has ***NO*** access to private properties of the instance
    Egg.prototype.Crack = function () { this.cracked = true; };
    
    
    var myEgg = new Egg("chicken", 2, 3, 500);
    
    myEgg.cost; // undefined
    myEgg.Crack();
    myEgg.cracked; // true
    

    That’s fine, but sometimes there are easier ways of getting around things.
    Sometimes you really don’t need a class.

    What if you just wanted to use one egg, ever, because that’s all your recipe called for?

    var myEgg = {};  // equals a new object
    myEgg.type = "ostrich";
    myEgg.cost = "......";
    myEgg.Crack = function () { this.cracked = true; };
    

    That’s great, but there’s still a lot of repetition there.

    var myEgg = {
        type : "ostrich",
        cost : "......",
        Crack : function () { this.cracked = true; }
    };
    

    Both of the two “myEgg” objects are exactly the same.

    The problem here is that EVERY property and EVERY method of myEgg is 100% public to anybody.

    The solution to that is immediately-invoking functions:

    // have a quick look at the bottom of the function, and see that it calls itself
    // with parens "()" as soon as it's defined
    var myEgg = (function () {
        // we now have private properties again!
        var cost, type, weight, cracked, Crack, //.......
    
        // this will be returned to the outside var, "myEgg", as the PUBLIC interface
        myReturnObject = {
            type : type,
            weight : weight,
            Crack : Crack, // added benefit -- "cracked" is now private and tamper-proof
            // this is how JS can handle virtual-wallets, for example
            // just don't actually build a financial-institution around client-side code...
            GetSaleValue : function () { return (cracked) ? 0 : cost; }
        };
    
        return myReturnObject;
    }());
    
    myEgg.GetSaleValue(); // returns the value of private "cost"
    myEgg.Crack();
    myEgg.cracked // undefined ("cracked" is locked away as private)
    myEgg.GetSaleValue(); // returns 0, because "cracked" is true
    

    Hope that’s a decent start.

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

Sidebar

Related Questions

I can see there are many ways to use jQuery safely in the javascript
I have a javascript object that contains a few objects that contain associative arrays.
Often I see javascript code where event handlers (like onmousemove) are assigned dynamically. Example:
In eclipse IDE, how can I see the javascript source code GWT has generated
Just for curiosity... If possible, where can I find and see the javascript submit()
I see quite a few different issues with the alert window and new lines.
Okay, I see a few references given for Java, but not javascript ( which
I have a few projects that are scripted via javascript. Not a web pages,
Often I see a global object defined in javascript code to act as a
when we write javascript in web page then other user can see that javascript

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.