# base/reflection (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/reflection/

Reflection functions. (presence checks, type checks, class inheritance checks, cloning, unique identifiers)

Example: Check types and clone

```js
import { Reflection, Vector2 } from "./build/vanilla.js";

const position = Vector2.create(3, 4);
const copied = Reflection.clone(position);
console.log(Reflection.isClassInstance(copied, Vector2), copied.x, copied.y);
console.log(Reflection.isPrimitiveType(123), Reflection.isArrayType([1, 2]));
console.log(Reflection.isValidate(null), Reflection.createGUID());
```

## Functions and constants

- `isValidate(target)` (method, since 0.2.1): Returns whether the object is valid.
  - `target` T: Value to check.
  - returns boolean: True if it is neither undefined nor null.
- `clone(target)` (method, since 0.0.8): Shallow clone of an instance.
  - `target` T: Object to clone. (throws an error if it is not an object or is missing)
  - returns T: A new object made by calling the same constructor with no arguments. (own properties and getter/setter pair properties are copied shallowly)
- `structuredClone(target)` (method, since 0.0.8): Deep clone of an instance.
  - `target` T: Object to clone. (throws an error if it is not an object or is missing)
  - returns T: A new object made by calling the same constructor with no arguments. (arrays and plain objects are copied deeply, class instance values keep their references)

  Example: Difference between shallow and deep clones

  ```js
  import { Reflection } from "./build/vanilla.js";
  
  class Inventory {
  	constructor() {
  		this.itemList = ["sword"];
  	}
  }
  
  const original = new Inventory();
  const shallowCopy = Reflection.clone(original);
  const deepCopy = Reflection.structuredClone(original);
  original.itemList.push("shield");
  console.log(shallowCopy.itemList.length, deepCopy.itemList.length);
  ```

- `createGUID()` (method, since 0.2.0): Generates a unique identifier.
  - "0192a3f2-b1c4-4d8e-8f1a-0e5d4c3b2a19"
  - returns string: A 36 character string shaped "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx". (the first 12 digits are the time in milliseconds)
- `isPrimitiveType(target)` (method, since 0.2.1): Returns whether it is a primitive type.
  - true for undefined / null / boolean / number / bigint / string / symbol.
  - e.g. isPrimitiveType(123) → true, isPrimitiveType({}) → false
  - `target` T: Value to check.
  - returns boolean: True if it is a primitive type.
- `isReferenceType(target)` (method, since 0.2.1): Returns whether it is a reference type.
  - object / array / function, etc. (excluding null).
  - e.g. isReferenceType({}) → true, isReferenceType(null) → false
  - `target` T: Value to check.
  - returns boolean: True if it is an object or a function. (false for null)
- `isArrayType(target)` (method, since 0.2.1): Returns whether it is an array type.
  - e.g. isArrayType([1, 2, 3]) → true, isArrayType("abc") → false
  - `target` T: Value to check.
  - returns boolean: True if it is an array.
- `isFunctionType(target)` (method, since 0.2.1): Returns whether it is a function type.
  - Regular functions / arrow functions / classes all have typeof "function", so true.
  - e.g. isFunctionType(() => {}) → true, isFunctionType(123) → false
  - `target` T: Value to check.
  - returns boolean: True if it is a function. (classes included)
- `isType(target)` (method, since 0.2.1): Returns whether it is a class/constructor function.
  - true if typeof is "function" and it has a prototype (excluding arrow functions).
  - e.g. isType(WorldNode) → true, isType(() => {}) → false
  - `target` T: Value to check.
  - returns boolean: True if it is a function that has a prototype.
- `isClassInstance(target, type)` (method, since 0.2.1): Returns whether it is an instance of a specific class.
  - true if target is an instance of type or one of its subtypes.
  - e.g. isClassInstance(new Vector2(), Vector2) → true
  - `target` T: Value to check.
  - `type` Function: Class.
  - returns boolean: True if target is an instance of type or one of its subclasses.
- `isParent(parentType, targetType)` (method, since 0.2.1): Returns whether the target class is a parent (ancestor) class of the type class.
  - true if type (directly or indirectly) inherits from target.
  - false when target === type.
  - e.g. isParent(TransformNode, WorldNode) → true
  - `parentType` Function: Class to check as the parent.
  - `targetType` Function: Class on the child side.
  - returns boolean: True if targetType inherits parentType directly or indirectly. (false for the same class)
- `isChildren(targetType, parentType)` (method, since 0.2.1): Returns whether the target class is a child (descendant) class of the type class.
  - true if target (directly or indirectly) inherits from type.
  - false when target === type.
  - e.g. isChildren(WorldNode, TransformNode) → true
  - `targetType` Function: Class to check as the child.
  - `parentType` Function: Class on the parent side.
  - returns boolean: True if targetType inherits parentType directly or indirectly. (false for the same class)
