By Piotr Sikora

  • javascript

  • 31 January 2014

Table of Contents

Sometimes you will need to create autoexecuted object (for example in case of load/document ready events).

Have you tried to do it this way?

(function() {
        // Elements
        var z;

        // Private methods
        var internalObj = {
            init: function() {
                console.log('init')
            }
        };

        // Autoinvoking constructor
        (function() {
            console.log('start');
            internalObj.init();

        })();

    })();

Autoexecuted object

So what will you do if you want to have external API to your “autoexecuted object”? You can do it this way:

var auto = (function() {
        // Elements
        var z;

        // Private methods
        var internalObj = {

            init: function() {
                console.log('init')
            }
        };

        // Autoinvoking constructor
        (function() {
            console.log('start');
            internalObj.init();

        })();
        return { initObj: internalObj.init}

    })();

auto.initObj();

Currently we are creating variable to which we are assigning an object. This object return an API with one method which is equal an internalObject init method.

But what we can do if we do not ant to do it this way and we don’t want to create any new object assigned to variable?

(function() {
        // Elements
        var z;

        // Private methods
        var internalObj = {

            init: function() {
                console.log('init')
            }
        };

        // Autoinvoking constructor
        (function() {
            console.log('start');
            internalObj.init();

        })();
        this.init = internalObj.init;

    })();

init();

So this.init = internalObj.init assigns to window (global) function a function from our internalObj - init.

Categories

Recent Posts

About Me

Piotr Sikora - Process Automation | AI | n8n | Python | JavaScript

Piotr Sikora

Process Automation Specialist

I implement automation that saves time and money, streamlines operations, and increases the predictability of results. Specializing in process automation, AI implementation, and workflow optimization using n8n, Python, and JavaScript.

n8n Workflows

n8n workflow automation templates

Explore my workflow templates on n8n. Ready-to-use automations for blog management, data collection, and AI-powered content processing.

3Workflow Templates

• Auto-Categorize Blog Posts with AI

• Collect LinkedIn Profiles

• Export WordPress Posts for SEO

Similar Articles

Discover more related content

Pure JavaScript: Private and Public Methods Guide

Have you been creating your own classes in pure JavaScript?

Raspberry Pi node.js and how to start with programming GPIO

Start programming Raspberry Pi GPIO with Node.js. Introduction to the PixPress project on GitHub. Hardware meets JavaScript.

Image to HTML table. Transform image to HTML table.

Image to HTML table. Transform image to HTML table.

I heard very interesting story about improvements in delivery of emailers.