"JavaScript hasCircularReference()"의 두 판 사이의 차이

3번째 줄: 3번째 줄:


<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
const hasCircularReference = (o, s = new WeakSet()) => !!o && typeof o === 'object' && (s.has(o) || (s.add(o), Object.values(o).some(v => hasCircularReference(v, s))));
function hasCircularReference(obj) {
    const seen = new WeakSet();
    const detect = obj => {
        if (obj && typeof obj === 'object') {
            if (seen.has(obj)) return true;
            seen.add(obj);
            return Object.values(obj).some(detect);
        }
        return false;
    }
    return detect(obj);
}


// 테스트 코드
// 테스트 코드
16번째 줄: 27번째 줄:
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
function hasCircularReference(obj, seen = new WeakSet()) {
function hasCircularReference(obj, seen = new WeakSet()) {
     return obj && typeof obj === 'object' && (seen.has(obj) || (seen.add(obj), Object.values(obj).some(value => hasCircularReference(value, seen))));
     if (obj && typeof obj === 'object') {
        if (seen.has(obj)) return true;
        seen.add(obj);
        return Object.values(obj).some(value => hasCircularReference(value, seen));
    }
    return false;
}
}


27번째 줄: 43번째 줄:
console.log(hasCircularReference(b)); // true
console.log(hasCircularReference(b)); // true
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
function hasCircularReference(obj, seen = new WeakSet()) {
function hasCircularReference(obj, seen = new WeakSet()) {
     if (obj && typeof obj === 'object') {
     return obj && typeof obj === 'object' && (seen.has(obj) || (seen.add(obj), Object.values(obj).some(value => hasCircularReference(value, seen))));
        if (seen.has(obj)) return true;
        seen.add(obj);
        return Object.values(obj).some(value => hasCircularReference(value, seen));
    }
    return false;
}
}


45번째 줄: 57번째 줄:
console.log(hasCircularReference(b)); // true
console.log(hasCircularReference(b)); // true
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
<syntaxhighlight lang='javascript' run>
function hasCircularReference(obj) {
const hasCircularReference = (o, s = new WeakSet()) => !!o && typeof o === 'object' && (s.has(o) || (s.add(o), Object.values(o).some(v => hasCircularReference(v, s))));
    const seen = new WeakSet();
    const detect = obj => {
        if (obj && typeof obj === 'object') {
            if (seen.has(obj)) return true;
            seen.add(obj);
            return Object.values(obj).some(detect);
        }
        return false;
    }
    return detect(obj);
}


// 테스트 코드
// 테스트 코드

2025년 2월 16일 (일) 14:25 판

개요

JavaScript hasCircularReference()
function hasCircularReference(obj) {
    const seen = new WeakSet();
    const detect = obj => {
        if (obj && typeof obj === 'object') {
            if (seen.has(obj)) return true;
            seen.add(obj);
            return Object.values(obj).some(detect);
        }
        return false;
    }
    return detect(obj);
}

// 테스트 코드
const a = { x: 1, y: 2 };
const b = { x: 1, y: 2 };
b.z = b; // 순환참조 생성

console.log(hasCircularReference(a)); // false
console.log(hasCircularReference(b)); // true
function hasCircularReference(obj, seen = new WeakSet()) {
    if (obj && typeof obj === 'object') {
        if (seen.has(obj)) return true;
        seen.add(obj);
        return Object.values(obj).some(value => hasCircularReference(value, seen));
    }
    return false;
}

// 테스트 코드
const a = { x: 1, y: 2 };
const b = { x: 1, y: 2 };
b.z = b; // 순환참조 생성

console.log(hasCircularReference(a)); // false
console.log(hasCircularReference(b)); // true
function hasCircularReference(obj, seen = new WeakSet()) {
    return obj && typeof obj === 'object' && (seen.has(obj) || (seen.add(obj), Object.values(obj).some(value => hasCircularReference(value, seen))));
}

// 테스트 코드
const a = { x: 1, y: 2 };
const b = { x: 1, y: 2 };
b.z = b; // 순환참조 생성

console.log(hasCircularReference(a)); // false
console.log(hasCircularReference(b)); // true
const hasCircularReference = (o, s = new WeakSet()) => !!o && typeof o === 'object' && (s.has(o) || (s.add(o), Object.values(o).some(v => hasCircularReference(v, s))));

// 테스트 코드
const a = { x: 1, y: 2 };
const b = { x: 1, y: 2 };
b.z = b; // 순환참조 생성

console.log(hasCircularReference(a)); // false
console.log(hasCircularReference(b)); // true
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}