JS โฉ values โฉ custom functions โฉ isPrimitive()
Copy โโโ (primitive ? )
โ typeof type expr value
---------------------------------------------------------------------
โ
object Null null null
---------------------------------------------------------------------
โ
undefined Undefined undefined undefined
---------------------------------------------------------------------
โ
number Number 37
Copy // โญ check if value is object
function isObject(value) {
return value === Object(value)
}
// โญ check if value is primitive
function isPrimitive(value) {
return !isObject(value)
}
// export
module.exports = { isObject, isPrimitive };
The Object constructor 's behavior depends on the input's type , if the value is
or ๏ผempty object is returned.
an object already๏ผthe value itself is returned.
otherwise๏ผobject of a Type that corresponds to the value is returned.
Object(value)
is identically to new Object(value)
Copy // โญ import (functions for type name)
const { typeName, baseTypeName } = require('./typeNames.js');
const { testCases } = require('./testCases.js');
const { isPrimitive } = require('./isPrimitive.js');
// table settings
const header = ['typeof', 'type', 'expr', 'value'];
const cols = header.length;
const [width, pad, ext] = [12, 1, 18];
const line = '-'.repeat(width*cols + pad*(cols-1) + ext);
// header
console.log(' ', ...header.map(s => s.padEnd(width, ' ')));
console.log(line);
// rows
for (const testCase of testCases) {
// separator
if (testCase === '---') {
console.log(line);
continue;
}
// test cases
const value = testCase[0];
const expr = testCase[1] || String(testCase[0]);
const types = [typeof value, typeName(value)];
const numberOfDifferentTypes = new Set(types.map(s => s.toLowerCase())).size;
console.log(
isPrimitive(value) ? 'โ
' : 'โ',
...[...types, expr].map(s => s.padEnd(width, ' ')),
value,
)
}
console.log(line);
// legend
console.log(`โข โญ๏ธ types not all the same โข โ base !== type`);
37
โ
number Number 3.14 3.14
โ
number Number Math . LN2 0.6931471805599453
โ
number Number Infinity Infinity โญ๏ธ
โ
number Number NaN NaN โญ๏ธ
โ
number Number Number ( '1' ) 1
โ
number Number Number ( 'ab' ) NaN
---------------------------------------------------------------------
โ
bigint BigInt 42 42 n
---------------------------------------------------------------------
โ
string String 'bla' 'bla'
โ
string String `x = ${ 1 + 2 } ` 'x = 3'
โ
string String typeof 1 'number'
โ
string String String ({}) '[object Object]'
โ
string String typeof xxx 'undefined'
---------------------------------------------------------------------
โ
boolean Boolean true true
โ
boolean Boolean Boolean ( 1 ) true
โ
boolean Boolean !! ( 1 ) true
---------------------------------------------------------------------
โ
symbol Symbol Symbol () Symbol ()
โ
symbol Symbol Symbol .iterator Symbol ( Symbol .iterator)
---------------------------------------------------------------------
โ object Object {a : 1 } { a : 1 }
โ object User user User { name : 'JohnDoe' }
โ object Array [ 1 , 2 ] [ 1 , 2 ]
โ object Date new Date () 2022 - 09 - 13 T01 : 47 :46.344 Z
โ object RegExp / regex / /regex/
---------------------------------------------------------------------
โ function Function function (){} [ Function (anonymous)]
โ function Function Math . sin [ Function : sin ]
โ function Function () => {} [ Function ( anonymous )]
โ function class class {} [ class ( anonymous )]
โ function class User [ class User ]
โ function GeneratorFunction function *(){} [ GeneratorFunction (anonymous)]
---------------------------------------------------------------------