mixin
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
a mixin is a object containing methods that can be used by other objects without a need to inherit from it.
๐็ฏไพ๏ผ
- pollyfill for
const str = 'abc';
const bool = true;
const num = 10;
const sym = Symbol('foo');
// โข primitives wrapped
// โข null and undefined (sources) ignored
const obj = Object.assign({}, str, null, bool, undefined, num, sym);
// only "string wrappers" have own enumerable properties.
console.log(obj); // { "0": "a", "1": "b", "2": "c" }
const { log } = console;
// ----------------------------------------------
// โ assign object to "null" (Error)
// โ TypeError:
// Cannot convert undefined or null to object
//
// let o1 = Object.assign(null, {a: 1});
// ^^^^ <----
// ----------------------------------------------
// โ
assign object to "number" (it's OK)
// "number" is wrapped to object.
let o2 = Object.assign(3, {a: 1});
let n = 5;
// --------------------------------------------
// โ TypeError:
// Cannot create property 'b' on number '5'
//
// n.b = 3;
// ^ <----
// --------------------------------------------
[
o2, // [Number: 3] { a: 1 }
typeof o2, // 'object' (โญ NOT 'number'โ)
o2 instanceof Number, // true (โญ still a `Number`โ)
o2.a, // 1
o2 == 3, // true
o2 === 3, // false (โญ NOT the same thingโ)
3 === 3, // true
o2 + 4, // 7
n + n.b, // NaN ( 5 + undefined )
typeof n, // 'number'
].forEach(x => log(x));