© 2019 Meouzer Consortium

JavaScript Data Typing Gone Wild
The Definitive Typing Function dtype()

loading
Classification cats' easy but Clasifacaiton JavaScrypt committeee members diff9kult becuase they arent cats. Meouzer

Mouser knows what he is talking about. JavaScript messed up type information because there isn't a single cat on the JavaScript committee. The native type was great for typing in ECMA-2009 but for ECMA-20015 they threw it all away for nothing because they don't have cats who would be curious enough to check things out. So coding the type() function in Data Typing in JavaScript isn't as Simple as you Think was possible only by mere luck and the JavaScript prowess of the cats of the Meouzer consortium.

Introduction

The predecessors of dtype() are type() and usertype(). userType() like type() should be useful to the programmer because it provides basic data typing infrastructure that JavaScript itself lacks. The code for userType() is expanded to obtain the dtype() typing function that types in great detail. However, the reader will have no use for dtype() most likely.

The dtype() function has two important roles. It provides the headers for the stringify() function so that deserialization can take place. It also detects objects that are not suitable for serialization.

Specification for dtype()

  1. If x is of a primitive data type then dtype(x) is the name of that data type.
  2. If x is a host object then dtype(x) is the native type of x.
  3. If x is the argument list of a function then dtype(x) is "Arguments".
  4. If Klass is a proper class then dtype(Klass.prototype) is "Klass[Prototype]".
  5. Assume there is a most derived proper class Klass for which x is an instance of Klass. If x is a class instance of Klass then dtype(x) is "Klass". But if x is a mere instance of Klass then dtype(x) is "Klass[Object(n)]" where n is the degree of the instance. However "(n)" is omitted if n is 1.
  6. Assume there is no proper class for which x is an instance. If x is a null object and n is the number of degrees of x from null then dtype(x) is "NullObject(n)". If x is a standard object then dtype(x) is "Object(n)" where n is the number of degrees of x from Object.prototype. In both cases, "(n)" is omitted if n is 1.

Examples

dtype(x)The most specific thing to be saidExample value of x
"Window"x is the windowwindow
"HTMLDocument"x is the documentwindow.document
"Boolean[Prototype]"x is the Boolean prototypeBoolean.prototype
"Boolean"x is a Boolean class instancenew Boolean(true)
"Boolean[Object]" x is a Boolean instance of degree 1. However type(x) is "Object" because x is not a Boolean class instance. Object.create(Boolean.Prototype)
"Boolean[Object(2)]" x is a Boolean instance of degree 2. However type(x) is "Object" because x is not a Boolean class instance. Object.create(new Boolean(true))
"Boolean[Object(2)]" x is a Boolean instance of degree 2. However type(x) is "Object" because x is not a Boolean class instance. Object.create(Object.create(Boolean.prototype))
"Function"x is a functionBoolean
"Function[Object(2)]" x is a Function instance of degree 2. However type(x) is "Object" because x is not a Function class instance. Object.create(Boolean)
"NullObject"x is a null object, 1 degree from nullObject.create(null)
"NullObject(2)"x is a null object, 2 degrees from nullObject.create(Object.create(null))
"Object"x is an object, 1 degree from Object.prototype{a:1}
"Object(2)"x is an object, 2 degrees from Object.prototypeObject.create({a:1})
"foo"x is an instace of the proper foo class one degree away from foo.prototype, and x itself is not a proper prototype. new foo(...)
"foo[Object(4)]"x is an instace of the proper foo class four degrees away from foo.prototype, and x is not a proper prototype. Use Object.create() on new foo() three times

The dtype() Function

Full code is at typing.js

The dtype() Function
function dtype(x) { const t = type(x); if(t !== "Object") return t; try { x.constructor; } catch(e) { return "CrossOriginObject";} const g = getClassPrototype(x); const P = g.classPrototype; const n = g.degree; if(P === null) return "NullObject" + ((n === 1)?"":"(" + n + ")"); if(P === x) return P.constructor.name + "[Prototype]"; if(P === Object.prototype) return "Object" + ((n === 1)?"":"(" + n + ")"); if(Object.getPrototypeOf(x) != P ) return P.constructor.name + "[Object" + "(" + n + ")" + "]"; return isNativeCode(P.constructor)? P.constructor.name + "[Object]": P.constructor.name; }