Skip to content

Instantly share code, notes, and snippets.

View wentout's full-sized avatar
😎
building teleport

Viktor Vershanskiy wentout

😎
building teleport
View GitHub Profile
const o = {
note: 'default note'
};
Object.defineProperty(o, 'letter', {
get: async function () {
return this.note;
},
set: async function (value) {
await new Promise((resolve) => {
@wentout
wentout / SummArity.js
Created August 13, 2020 14:30
Summ With Arity
const summ = function (...args) {
let arity = 0;
let storage = [];
const isNumber = (el) => {
return (this instanceof Number) || typeof el === 'number' && !Number.isNaN(el);
};
if (isNumber(this)) {
arity = 0 + this;
@wentout
wentout / ArityFromCallback.js
Last active August 16, 2020 21:40
Arity From Callback
const ArityWithCallback = function (callback) {
// callback length is number of arguments
// that callback can accept before
// it has to be called
const arity = callback.length;
const storage = Array.isArray(this) ? this : [];
if (arity === 0) {
return callback;
@wentout
wentout / DerivedFromNumber.js
Last active August 17, 2020 16:04
An Example how an Object might be derived from new Number
const DerivedPrimitiveFactory = function (existentInstance, ModificatorType) {
const TripleSchemeClosure = function () {
ModificatorType.prototype = this;
ModificatorType.prototype.constructor = ModificatorType;
return ModificatorType;
};
@wentout
wentout / ExplorableDFD.js
Last active August 21, 2020 13:31
// Explorable! Document or Data Flow Definition...
// explorable Document Flow Definition
const NUMBER_2_EXPLORE_FROM = new Number(5);
const someData4DFD = [5, 4, 3, 2, 1];
const ExplorableDFD = function (explorable) {
const proto = Object.getPrototypeOf(this);
const protoValue = proto.valueOf();
debugger;
@wentout
wentout / NullPrototype.js
Last active August 21, 2020 21:48
representing null as constructor prototype
'use strict';
const MyNull = function () {};
MyNull.prototype = null;
const s = new MyNull();
// as this might be only the object,
// so null would be converted
console.log(s);
console.log(s.valueOf()); // {}
@wentout
wentout / InstanceAsFunction.js
Last active September 1, 2020 18:56
an example of instance as a callable function
class MyClass {}
class FunctionAsInstance extends MyClass {
constructor(...args) {
super();
const me = this;
this.args = args;
@wentout
wentout / NumberProxy.js
Last active September 2, 2020 19:54
Primitive Vector Magic Proxy Getter )
'use strict';
const vectorObj = new Number(5);
const proxyAsNumber = new Proxy(vectorObj, {
get (target, prop) {
if (prop === Symbol.toPrimitive) {
return function (...args) {
// this -- proxy itself
// args === ['default']
@wentout
wentout / NumbersProxyChain.js
Created September 7, 2020 09:23
// Number -> Proxy Life Cycle
// Number -> Proxy Life Cycle
const ogp = Object.getPrototypeOf;
var a = new Number(5);
a.extract = function () {
return a.valueOf();
};
console.log(a.extract());
@wentout
wentout / NumbersIncrementalComputation.js
Last active September 7, 2020 20:47
Incremental Computation with Prototype Chain
// there is Object [null prototype]
// under the hood, so we have room there
var a = new Number(5);
a.extract = function () {
return a.valueOf();
};