freeze-class

class freezer

Usage no npm install needed!

<script type="module">
  import freezeClass from 'https://cdn.skypack.dev/freeze-class';
</script>

README

freeze-class npm version Build Status

freezer.jpg

About:

Freeze a class and its prototype.

Note: this module is deprecated, please use the object-based module subzero

install:

npm install freeze-class

usage:

'use strict';

const freezeClass = require( 'freeze-class' );

class C {

    static f() { return 69; }

    f() { return 22; }
}

freezeClass( C );

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

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

/*
    and 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;

optional usage:

letitgo.gif

Deep freeze a class in 'deep' mode. This will not freeze any classes within the class. It works by recursively freezing anything of type "object".

'use strict';

const freezeClass = require( 'freeze-class' );

class C {}

class InnerClass {}

InnerClass.x = {

    y: {}
};

C.a = {

    b: {

        c: {

            d: {

                InnerClass
            }
        }
    }
};

C.prototype.x = {

    y: {

        z: {}
    },

    w: {}
};

freezeClass( C, 'deep' );

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

// Object.isFrozen( C );
// Object.isFrozen( C.a );
// Object.isFrozen( C.a.b );
// Object.isFrozen( C.a.b.c );
// Object.isFrozen( C.a.b.c.d );
// Object.isFrozen( C.prototype );
// Object.isFrozen( C.prototype.x );
// Object.isFrozen( C.prototype.x.y );
// Object.isFrozen( C.prototype.x.y.z );
// Object.isFrozen( C.prototype.x.w );
// !Object.isFrozen( InnerClass );
// !Object.isFrozen( InnerClass.x );
// !Object.isFrozen( InnerClass.x.y );

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

Deep freeze an object in 'deep' mode. This will not freeze any classes within the object. It works by recursively freezing anything of type "object".

'use strict';

const freezeClass = require( 'freeze-class' );

const o = {};

class InnerClass {}

InnerClass.x = {

    y: {}
};

o.a = {

    b: {

        c: {

            d: {

                InnerClass
            }
        }
    }
};

o.x = {

    y: {

        z: {}
    },

    w: {}
};

freezeClass( o, 'deep' );

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

// Object.isFrozen( o );
// Object.isFrozen( o.a );
// Object.isFrozen( o.a.b );
// Object.isFrozen( o.a.b.c );
// Object.isFrozen( o.a.b.c.d );
// Object.isFrozen( o.x );
// Object.isFrozen( o.x.y );
// Object.isFrozen( o.x.y.z );
// Object.isFrozen( o.x.w );
// !Object.isFrozen( InnerClass );
// !Object.isFrozen( InnerClass.x );
// !Object.isFrozen( InnerClass.x.y );