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
##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):
Note: the deep freeze function was inspired by MDN. The mega freeze function was inspired by deep-freeze.
####No-op If the input value is not an object, a function, or a class for any of subzero's functions, the return value will be the input value unaltered.
###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;
βοΈπ
πΏπ
π½π
πΎπ
πΌβοΈπΏπ»π
###2) subzero.deepFreeze( functionOrClassOrObjectToFreeze )
Deep freeze a class, an object, or a function. This will not freeze any classes or functions within the input object/function/class. It works by recursively freezing anything of type "object".
'use strict';
const subzero = require( 'subzero' );
/*
this example uses an object, subzero.deepFreeze also works
with functions and classes
*/
const o = {};
class InnerClass {}
InnerClass.x = {
y: {}
};
function innerFunction() {}
innerFunction.x = {
y: {}
};
const willBeFrozen = {};
o.a = {
b: {
c: {
d: {
InnerClass,
innerFunction,
e: Object.freeze({ willBeFrozen })
}
}
}
};
o.x = {
y: {
z: {}
},
w: {}
};
const reference = subzero.deepFreeze( o );
/*
the following statements will now return true:
*/
// ( reference === o );
// 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 );
// !Object.isFrozen( innerFunction );
// !Object.isFrozen( innerFunction.x );
// !Object.isFrozen( innerFunction.x.y );
// Object.isFrozen( willBeFrozen );
###3) 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", if they are unfrozen. Note the * MEGA FREEZE CORNER CASE.
'use strict';
/*
this example uses a function, subzero.megaFreeze also works
with objects and classes
*/
function f() {}
class InnerClass {};
InnerClass.x = {
y: {}
};
const innerFunction = function() {};
innerFunction.x = {
y: {}
};
/* MEGA FREEZE CORNER CASE:
be careful about objects/functions within already frozen objects and functions
*/
const wontBeFrozen = {};
f.a = {
b: {
c: {
d: {
InnerClass,
innerFunction,
e: Object.freeze({ wontBeFrozen })
}
}
}
};
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.x );
// Object.isFrozen( innerFunction.x.y );
// Object.isFrozen( innerFunction.prototype );
// !Object.isFrozen( wontBeFrozen );



