JSPM

  • Created
  • Published
  • Downloads 90
  • Score
    100M100P100Q56400F
  • License ISC

class, function, and object freezer

Package Exports

  • subzero

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (subzero) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

#subzero npm version Build Status

mksfw.gif

##About:

Freeze a class, freeze a function, or freeze an object.

##install:

npm install subzero

##usage:

####function list (each function explained in more detail below):

  1. subzero.freeze
  2. subzero.megaFreeze

Note: the mega freeze function was inspired by MDN and deep-freeze.


β„οΈπŸŽ…πŸΏπŸŽ…πŸ½πŸŽ…πŸΎπŸŽ…πŸΌβ›„οΈπŸŽΏπŸ—»πŸ‚

####No-op If the input value is not an object, a function, or a class for either of subzero's functions, the return value will be the input value unaltered.

letitgo.gif

###1) subzero.freeze( functionOrClassOrObjectToFreeze ) Freeze a class or a function and its prototype. For object input, it acts the exact same way as Object.freeze.

'use strict';

const subzero = require( 'subzero' );

/*
    this example uses a class,
    subzero.freeze also works with objects and functions
*/
class C {

    static f() { return 69; }

    f() { return 22; }
}

const reference = subzero.freeze( C );

/*
    the following statements will now return true:
*/

// ( reference === C );
// Object.isFrozen( C );
// Object.isFrozen( C.prototype );

/*
    as a result the following statements will now throw TypeErrors in strict mode:
*/

// C.g = function() { return 42 };
// C.f = function() { return 42 };
// delete C.f;
// C.constant = 42;
// C.prototype.g = function() { return 42 };
// C.prototype.f = function() { return 42 };
// delete C.prototype.f;
// C.prototype.constant = 42;

freezer.jpg

###2) subzero.megaFreeze( functionOrClassOrObjectToFreeze ) Deep freeze a class, an object, or a function. This will freeze any classes, functions, and objects within the class. It works by recursively freezing anything of type "object" or "function".

'use strict';

const subzero = require( 'subzero' );

/*
    this example uses a function,
    subzero.megaFreeze also works with objects and classes
*/
function f() {}

const InnerClass = class {};

InnerClass.x = {

    y: {}
};

const innerFunction = function() {};

innerFunction.x = {

    y: {}
};

const objectInsideAlreadyFrozenObject = {};

const functionInsideAlreadyFrozenObject = function() {};

const objectInsideAlreadyFrozenFunction = {};

const functionInsideAlreadyFrozenFunction = function() {};

const functionToFreeze = function() {};

functionToFreeze.objectInsideAlreadyFrozenFunction = objectInsideAlreadyFrozenFunction;

functionToFreeze.functionInsideAlreadyFrozenFunction = functionInsideAlreadyFrozenFunction;

const frozenFunctionWithNonFrozenObjectAndFunctionInside = Object.freeze( functionToFreeze );

f.a = {

    b: {

        c: {

            d: {

                InnerClass,

                innerFunction,

                e: Object.freeze({

                    objectInsideAlreadyFrozenObject,

                    functionInsideAlreadyFrozenObject
                }),

                frozenFunctionWithNonFrozenObjectAndFunctionInside
            }
        }
    }
};

f.prototype.x = {

    y: {

        z: {}
    },

    w: {}
};

const reference = subzero.megaFreeze( f );

/*
    the following statements will now return true:
*/

// ( reference === f );
// Object.isFrozen( f );
// Object.isFrozen( f.a );
// Object.isFrozen( f.a.b );
// Object.isFrozen( f.a.b.c );
// Object.isFrozen( f.a.b.c.d );
// Object.isFrozen( f.prototype );
// Object.isFrozen( f.prototype.x );
// Object.isFrozen( f.prototype.x.y );
// Object.isFrozen( f.prototype.x.y.z );
// Object.isFrozen( f.prototype.x.w );
// Object.isFrozen( InnerClass );
// Object.isFrozen( InnerClass.prototype );
// Object.isFrozen( InnerClass.x );
// Object.isFrozen( InnerClass.x.y );
// Object.isFrozen( innerFunction );
// Object.isFrozen( innerFunction.prototype );
// Object.isFrozen( innerFunction.x );
// Object.isFrozen( innerFunction.x.y );
// Object.isFrozen( objectInsideAlreadyFrozenObject );
// Object.isFrozen( objectInsideAlreadyFrozenFunction );
// Object.isFrozen( functionInsideAlreadyFrozenObject );
// Object.isFrozen( functionInsideAlreadyFrozenObject.prototype );
// Object.isFrozen( functionInsideAlreadyFrozenFunction );
// Object.isFrozen( functionInsideAlreadyFrozenFunction.prototype );

frieza22.gif