/ Gists / Function object with inside small functions
On gists

Function object with inside small functions

JavaScript-OOP JavaScript

example.js Raw #

var Wrapper = (function () {
	this.A = () => {
		console.log('A');
	}
	this.B = () => {
		console.log('B');
	}
	
	return {
		A2: this.A,
		B2: this.B
	}

	// or return this
})();


Wrapper.B2(); // B

DecimalPrecision.js Raw #

var DecimalPrecision = (function () {
	if (Number.EPSILON === undefined) {
		Number.EPSILON = Math.pow(2, -52);
	}
	this.round = function (n, p = 2) {
		let r = 0.5 * Number.EPSILON * n;
		let o = 1;
		while (p-- > 0) o *= 10;
		if (n < 0) o *= -1;
		return Math.round((n + r) * o) / o;
	};
	this.ceil = function (n, p = 2) {
		let r = 0.5 * Number.EPSILON * n;
		let o = 1;
		while (p-- > 0) o *= 10;
		if (n < 0) o *= -1;
		return Math.ceil((n + r) * o) / o;
	};
	this.floor = function (n, p = 2) {
		let r = 0.5 * Number.EPSILON * n;
		let o = 1;
		while (p-- > 0) o *= 10;
		if (n < 0) o *= -1;
		return Math.floor((n + r) * o) / o;
	};
	return this;
})();