
WeakMap::offsetGet 메소드는 WeakMap 객체의 특정 키(key)에 해당하는 값을 반환합니다. 만약 key가 존재하지 않으면 undefined를 반환합니다. 그러나, 특수한 경우에 다른 값을 반환할 수 있습니다.
예를 들어, WeakMap::offsetGet 메소드는 null이나 undefined를 key로 사용할 수 있습니다. 이 경우, null이나 undefined를 반환합니다.
또한, WeakMap::offsetGet 메소드는 WeakMap 객체의 프로퍼티를 읽을 때도 사용됩니다. 만약 프로퍼티가 존재하지 않으면 undefined를 반환합니다. 그러나, 프로퍼퍼티가 존재하지 않더라도, 프로퍼티가 존재하지 않음을 나타내는 undefined가 아닌 다른 값이 반환될 수 있습니다.
예를 들어, WeakMap::offsetGet 메소드는 프로퍼티가 존재하지 않아도 undefined가 아닌 null을 반환할 수 있습니다.
#hostingforum.kr
javascript
const weakMap = new WeakMap();
console.log(weakMap.get(null)); // null
또한, WeakMap::offsetGet 메소드는 Proxy 객체를 사용할 때도 유용합니다. 예를 들어, Proxy 객체의 trap을 설정했을 때, WeakMap::offsetGet 메소드는 trap을 호출하여 값을 반환할 수 있습니다.
#hostingforum.kr
javascript
const weakMap = new WeakMap();
const proxy = new Proxy({}, {
get: (target, key) {
return 'custom value';
}
};
console.log(weakMap.get(proxy)); // custom value
결과적으로, WeakMap::offsetGet 메소드는 특수한 상황에서 다른 값을 반환할 수 있습니다. 이에 대한 예제를 살펴보면, 다음과 같이 나타낼 수 있습니다.
#hostingforum.kr
javascript
const weakMap = new WeakMap();
console.log(weakMap.get(null)); // null
console.log(weakMap.get(undefined)); // undefined
const proxy = new Proxy({}, {
get: (target, key) {
return 'custom value';
}
};
console.log(weakMap.get(proxy)); // custom value
2025-04-19 10:49