Categories: Uncategorized

javascript – namespacing, public and private data

This is quick code sample of public and private data using javascript. You would create anonymous call back function. Private data would be store in var.
Public data would be returned.

// we create anonymous callback function to create private and public functions
var THINGS = function() {
// private
var gifts = 3;
// public
return {
    gems: 1000,
    gold: 2000,
    swords: 300,
    SECRET: {
      open: function() {
        gifts--;
        alert('OPENED!');
      }
    }
};}();

I hope you found this code snippet useful in your coding. These are higher order examples of code has to deal with object orientated programming. You can apply these concepts elsewhere.

Public functions can be access and changed anywhere. Where as private can’t be changed the numbers stay static. For example gifts will stay 3, but the public function can subtract it in increments of negative 1.

The reason behind public and private is track bugs. If you have too many public/global variables you get lost quick and other variables can overwrite those current variables, which is something you don’t want. Plus there is name spacing here the namespace is THINGS.

Admin

Recent Posts

Unveiling the Intricacies of the Tinder Algorithm: 5 Surprising Metrics

Monitored by Tinder The inner workings of the Tinder algorithm remain shrouded in secrecy, known…

3 weeks ago

Is Shopping Temu Safe

Use fav19073 30% OFF Shop Now I recently had a fantastic buying experience on Temu,…

4 months ago

Cheap Netflix Subscription

Unlocking Affordable Streaming: How Gamsgo.com is Revolutionizing Netflix Subscriptions Sign Up Save1 coupon code for…

4 months ago

Domains for SALE.

razerkeyb.com gloriousmouse.com

9 months ago

How to Start your own vegetable garden?

Planting your own vegetables and fruit is a great way to save money and eat…

1 year ago

Jobs AI can’t replace

While AI has the potential to automate many tasks, there are still some jobs that…

1 year ago