getButterfly
Resume

NAVIGATION

ATTRIBUTION

getButterfly
Dublin, Ireland

All Content Copyright ©2019
getButterfly

Encapsulation: Private Attribute

In JavaScript OOP there are attributes called private attributes. Knowing if an attribute is private or public, an API user can focus on the API as a whole.

Let’s create some private attributes.

(function() {
    var private_id = guid();

    var init = function() {
        // Private attribute holder
        this[private_id] = {};

        // Create_time is a private attribute
        this[private_id].creation_time = (new Date()).getTime();
    };

    // This is the public method accessing the private attribute
    var get_creation_time = function() {
        return this[private_id].creation_time;
    };

    // Anything listed below are public methods
    My_Class = Class.extend({
        init: init,
        get_creation_time: get_creation_time
    });
})();

Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.

Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.

Added by Ciprian on Monday, January 15, 2018 in Blog, JavaScript

Do you want better SEO? More traffic? More conversions? More growth? We help companies exponentially grow their traffic and conversions, while outranking their competitors. With more than 10 years of experience, we’ve learned what is valuable to our clients.
SEO Dublin | SEO Malta

My Battle Tested Recommendations

Jetpack
Dreamhost
WordPress.com
CodeCanyon
SEMrush

Disclaimer: These recommendations contain affiliate links.


Privacy Policy