# base/reflection (vanilla.js 0.4.0)

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

리플렉션 함수 모음. (값이 있는지, 꼴 검사, 클래스 상속 검사, 복제, 고유식별자)

예제: 꼴 검사하고 복제하기

```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): 유효한 객체인지 여부 반환.
  - `target` T: 검사할 값.
  - returns boolean: undefined 나 null 이 아니면 참.
- `clone(target)` (method, since 0.0.8): 인스턴스 얕은 복제.
  - `target` T: 복제할 객체. (객체가 아니거나 없으면 오류를 던진다)
  - returns T: 같은 생성자를 인자 없이 불러 만든 새 객체. (자기 속성과 getter, setter 짝 속성을 얕게 옮긴다)
- `structuredClone(target)` (method, since 0.0.8): 인스턴스 깊은 복제.
  - `target` T: 복제할 객체. (객체가 아니거나 없으면 오류를 던진다)
  - returns T: 같은 생성자를 인자 없이 불러 만든 새 객체. (배열과 일반 객체는 깊게, 클래스 인스턴스 값은 참조 그대로 옮긴다)

  예제: 얕은 복제와 깊은 복제의 차이

  ```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): 고유식별자 생성.
  - "0192a3f2-b1c4-4d8e-8f1a-0e5d4c3b2a19"
  - returns string: "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" 꼴의 36 글자 문자열. (앞 12 자리는 밀리초 시각)
- `isPrimitiveType(target)` (method, since 0.2.1): 원시 타입 여부 반환.
  - undefined / null / boolean / number / bigint / string / symbol 이면 true.
  - 예) isPrimitiveType(123) → true, isPrimitiveType({}) → false
  - `target` T: 검사할 값.
  - returns boolean: 원시 타입이면 참.
- `isReferenceType(target)` (method, since 0.2.1): 참조 타입 여부 반환.
  - object / array / function 등 (null 제외).
  - 예) isReferenceType({}) → true, isReferenceType(null) → false
  - `target` T: 검사할 값.
  - returns boolean: 객체나 함수면 참. (null 은 거짓)
- `isArrayType(target)` (method, since 0.2.1): 배열 타입 여부 반환.
  - 예) isArrayType([1, 2, 3]) → true, isArrayType("abc") → false
  - `target` T: 검사할 값.
  - returns boolean: 배열이면 참.
- `isFunctionType(target)` (method, since 0.2.1): 함수 타입 여부 반환.
  - 일반 함수 / 화살표 함수 / 클래스 모두 typeof 가 "function" 이므로 true.
  - 예) isFunctionType(() => {}) → true, isFunctionType(123) → false
  - `target` T: 검사할 값.
  - returns boolean: 함수면 참. (클래스 포함)
- `isType(target)` (method, since 0.2.1): 클래스/생성자 함수 여부 반환.
  - typeof 가 "function" 이고 prototype 을 가진 경우 true (화살표 함수 제외).
  - 예) isType(WorldNode) → true, isType(() => {}) → false
  - `target` T: 검사할 값.
  - returns boolean: prototype 을 가진 함수면 참.
- `isClassInstance(target, type)` (method, since 0.2.1): 특정 클래스의 인스턴스 여부 반환.
  - target 이 type 또는 그 하위 타입의 인스턴스이면 true.
  - 예) isClassInstance(new Vector2(), Vector2) → true
  - `target` T: 검사할 값.
  - `type` Function: 클래스.
  - returns boolean: target 이 type 이나 그 하위 클래스의 인스턴스면 참.
- `isParent(parentType, targetType)` (method, since 0.2.1): target 클래스가 type 클래스의 부모(상위) 클래스인지 여부 반환.
  - type 이 target 을 (직간접적으로) 상속하면 true.
  - target === type 인 경우는 false.
  - 예) isParent(TransformNode, WorldNode) → true
  - `parentType` Function: 부모인지 볼 클래스.
  - `targetType` Function: 자식 쪽 클래스.
  - returns boolean: targetType 이 parentType 을 직간접으로 상속하면 참. (같은 클래스면 거짓)
- `isChildren(targetType, parentType)` (method, since 0.2.1): target 클래스가 type 클래스의 자식(하위) 클래스인지 여부 반환.
  - target 이 type 을 (직간접적으로) 상속하면 true.
  - target === type 인 경우는 false.
  - 예) isChildren(WorldNode, TransformNode) → true
  - `targetType` Function: 자식인지 볼 클래스.
  - `parentType` Function: 부모 쪽 클래스.
  - returns boolean: targetType 이 parentType 을 직간접으로 상속하면 참. (같은 클래스면 거짓)
