Skip to content

Instantly share code, notes, and snippets.

@mikedfunk
Created November 4, 2013 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikedfunk/7306880 to your computer and use it in GitHub Desktop.
Save mikedfunk/7306880 to your computer and use it in GitHub Desktop.
JavaScript Snippets

Javascript Public, Private Instance Methods

    
    var array = [];
    
    var object = {};
    
    var method = function () {};
    
    var instance = new function (privateField, a, b) {
        // allows all instance methods and fields to have access to the current instance.
        var me = this;
    
        // creates a private method, only accesible to methods inside instance
        function privateMethod() {
        }
    
        // creates a private method, accessible to only methods inside instance
        function privateMethod() {
            // callbacks and private methods dont have access to `this`, so we use `me`
            this.publicMethod(); // bad
            me.publicMethod(); // good
        }
    
        // creates a public method, accessible to methods inside and outside the instance
        this.publicMethod = function () {
            // public methods can call private methods
            privateMethod();
        };
    
        // creates a public field.
        this.publicField = privateField;
    
        // creates public fields referring to private fields.
        this.b = a;
        this.a = b;
    };
    
    // can not call private instance methods
    instance.privateMethod(); // bad
    
    // can call public instance methods.
    instance.publicMethod(); // good

Map and Reduce Example

    // create a map function
    var map = function (array, callback) {
        // initialize an empty results array
        var results = [];

        // loop through each element in the passed array, run it through the callback method, add it to the results array
        for (var i in array) {
            results[i] = callback(array[i], i);
        }

        return results;
    };

    // create a reduce function with the starting value, array of things to callback through, 
    // and the function to mess with the array
    var reduce = function (initial, array, callback) {
        // set the starting value
        var result = initial;

        // loop through the passed array, modify the current result value with the callback
        for (var i in array) {
            result = callback(result, array[i], i);
        }

        return result;
    };

    // create an example function which applies map and reduce
    var sum = function (values, callback) {
        
        return reduce(
            // set the starting value
            0,
            
            // let the map function pass through the value and callback
            map(
                values,
                callback
            ),
            
            // this is the callback for reduce. It just adds the current value of the mapped array to the 
            // current total, which is why this function is called sum.
            function (result, value) {
                return result + value;
            }
        );
    };

    // log to the browser console
    console.log(
        
        // call our example sum function
        sum(
            // specify an array of starting numbers
            [5, 6, 7],
            
            // specify a callback to optionally transform each element of this array with map()
            function (value) {
                // this function doesn't do any transforming before summing up
                return value;
            }
            // sum will take care of adding each item to the result and returning the result.
        )
    );
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment