programing

빈 어레이는 true와 false를 동시에 나타내는 것처럼 보인다.

goodcopy 2023. 1. 10. 21:28
반응형

빈 어레이는 true와 false를 동시에 나타내는 것처럼 보인다.

빈 어레이는 참이지만 false와도 같습니다.

var arr = [];
console.log('Array:', arr);
if (arr) console.log("It's true!");
if (arr == false) console.log("It's false!");
if (arr && arr == false) console.log("...what??");

이것은 등가 연산자에 의한 암묵적인 변환에 의한 것이라고 생각합니다.

뒤에서 무슨 일이 벌어지고 있는지 설명해 줄 사람?

다른 걸 테스트하고 있잖아

if (arr)호출된 객체(Array는 JS에서 객체의 인스턴스)는 객체가 존재하는지 확인하고 true/false를 반환합니다.

했을 때if (arr == false)이 객체와 원시 객체의 값을 비교합니다.false치치. 부 value value value,arr.toString()호출되어 빈 "이 됩니다.""

그 이유는toString는 " "를 반환합니다.Array.join()자바스크립트

회선에 대해서:

if (arr == false) console.log("It's false!");

도움이 될 수 있습니다.

console.log(0 == false) // true
console.log([] == 0) // true
console.log([] == "") // true

일어나고 건 부울이false0개체(왼쪽)와의 비교를 위해 사용합니다.오브젝트는 문자열(빈 문자열)로 강제됩니다.그런 다음 빈 문자열도 숫자(즉, 0)로 강제됩니다. 마지막 는 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★」0==0, 「」)true.

편집: 이 기능의 상세한 것에 대하여는, 사양의 이 섹션을 참조해 주세요.

규칙 1부터 시작하는 상황은 다음과 같습니다.

1. 타입(x)이 타입(y)과 다를 경우 스텝14로 넘어갑니다.

적용되는 다음 규칙은 19번입니다.

19. 유형(y)이 부울인 경우 x == ToNumber(y) 비교 결과를 반환합니다.

ToNumber(false)0하다

[] == 0

다시 규칙 #1에서는 스텝 #14로 건너뛰도록 되어 있지만 실제로 적용되는 다음 단계는 #21입니다.

21. Type(x)이 Object이고 Type(y)이 String 또는 Number이면 비교 결과를 ToPrimitive(x)== y로 반환합니다.

ToPrimitive([])이 있습니다.

"" == 0

다시 규칙 #1에서는 스텝 #14로 건너뛰도록 되어 있지만, 실제로 적용되는 다음 단계는 #17입니다.

17. 유형(x)이 문자열이고 유형(y)이 숫자인 경우 ToNumber(x)== y 비교 결과를 반환합니다.

ToNumber("")0남습니다.

0 == 0

두 값 모두 유형이 같기 때문에 스텝은 #1에서 #7까지 계속됩니다.이 스텝은 다음과 같습니다.

7. x가 y와 같은 숫자 값이면 true를 반환합니다.

해서 다시 돌려보냅니다.true.

요약:

ToNumber(ToPrimitive([])) == ToNumber(false)

웨인의 대답을 보충하고 왜 그런지 설명하려고 노력하다ToPrimitive([])"", '왜' 질문에 대한 두 가지 가능한 답을 고려할 가치가 있다.첫 번째 답변 유형은 "스펙에 따르면 JavaScript가 이렇게 동작하기 때문입니다."입니다.ES5 사양 섹션 9.1에서는 ToPrimitive의 결과를 개체의 기본값으로 설명합니다.

객체의 기본값은 객체의 [DefaultValue] 내부 메서드를 호출하여 옵션의 힌트 PreferredType을 전달함으로써 가져옵니다.

섹션 8.12.8에서는[[DefaultValue]] 는 " 또는Number 중 입니다.힌트 스트링 넘버 String을 합니다.[[DefaultValue]]에 의해 값이 반환됩니다.toString()되고 그 의 경우 됩니다.valueOf()보다 우선 순위가 toString() ★★★★★★★★★★★★★★★★★」valueOf() 하다valueOf()원시적인 경우에는 먼저 호출되고 그 값이 반환됩니다.라,든 따 thus든[[DefaultValue]]toString() ★★★★★★★★★★★★★★★★★」valueOf()는 오브젝트에 대해 지정된 PreferredType 및 이러한 함수가 기본 값을 반환하는지 여부에 따라 달라집니다.

" " "valueOf()개체 방법은 기본 방법을 재정의하지 않으면 기본 방법을 재정의할 수 있는 개체를 반환합니다. 즉 체 는 서 클 환 드 를 object method, overrides method itself returns the, that덮본 a다반메valueOf()객체 자체를 반환합니다.개체 자체를 반환합니다. This is the case for 이 경우Array[].valueOf()개체를 반환합니다.[]자, 자, 아니에요! - 왜요?그 자체입니다. Since an 그 이후로는Array인 것이 .[[DefaultValue]]은 "반환" .어레이의 반환값은 다음 값이 됩니다.toString().

David Flanagan의 JavaScript를 인용하면: 참고로 이 가이드는 다음과 같은 질문에 대한 답을 얻을 수 있는 최고의 책입니다.

이 오브젝트 대 번호 변환의 자세한 내용은 빈 배열이 숫자 0으로 변환되는 이유와 단일 요소를 가진 배열도 숫자로 변환되는 이유를 설명합니다.어레이는 기본값이 아닌 객체를 반환하는 기본값 Of() 메서드를 상속하기 때문에 어레이에서 번호로의 변환은 toString() 메서드에 의존합니다.빈 배열은 빈 문자열로 변환됩니다.그리고 빈 문자열은 숫자 0으로 변환됩니다.단일 요소가 포함된 배열은 한 요소가 변환하는 문자열과 동일합니다.배열에 단일 번호가 포함된 경우 해당 숫자는 문자열로 변환된 다음 숫자로 다시 변환됩니다.

"질문에 두 은 "말하기 에"를 왜 그에서 이치에 합니다.왜"라는 질문에 대한 두 번째 유형의 답변은 "스펙이 말하기 때문에"를 제외하고 설계 관점에서 동작이 왜 이치에 맞는지에 대해 설명합니다.이 문제에 대해 나는 추측만 할 수 있다.먼저 어레이를 숫자로 변환하려면 어떻게 해야 할까요?유일하게 생각할 수 있는 것은 빈 어레이를 0으로, 비어 있지 않은 어레이를 1로 변환하는 것입니다.그러나 Wayne의 답변에서 알 수 있듯이 빈 배열은 여러 유형의 비교를 위해 0으로 변환됩니다.이 밖에도 Array.valueOf()에 대한 합리적인 프리미티브 리턴 값은 생각하기 어렵습니다.그래서 어떤 이는 이 모든 것이 이치에 맞는다고 주장할 수 있다.Array.valueOf(), 자체를 은, 「Default」를 선두는 「어레이」입니다.toString()Primitive에게어레이를 숫자가 아닌 문자열로 변환하는 것이 더 효과적입니다.

게다가 Flanagan의 인용에서 암시되었듯이, 이 설계 결정은 특정 유형의 유익한 행동을 가능하게 합니다.예:

var a = [17], b = 17, c=1;
console.log(a==b);      // <= true
console.log(a==c);      // <= false

이 동작을 통해 단일 요소 배열을 숫자와 비교하여 예상 결과를 얻을 수 있습니다.

console.log('-- types: undefined, boolean, number, string, object --');
console.log(typeof undefined);  // undefined
console.log(typeof null);       // object
console.log(typeof NaN);        // number
console.log(typeof false);      // boolean
console.log(typeof 0);          // number
console.log(typeof "");         // string
console.log(typeof []);         // object
console.log(typeof {});         // object

console.log('-- Different values: NotExist, Falsy, NaN, [], {} --');
console.log('-- 1. NotExist values: undefined, null have same value --');
console.log(undefined == null); // true

console.log('-- 2. Falsy values: false, 0, "" have same value --');
console.log(false == 0);        // true
console.log(false == "");       // true
console.log(0 == "");           // true

console.log('-- 3. !NotExist, !Falsy, and !NaN return true --');
console.log(!undefined);        // true
console.log(!null);             // true

console.log(!false);            // true
console.log(!"");               // true
console.log(!0);                // true

console.log(!NaN);              // true

console.log('-- 4. [] is not falsy, but [] == false because [].toString() returns "" --');
console.log(false == []);       // true
console.log([].toString());     // ""

console.log(![]);               // false

console.log('-- 5. {} is not falsy, and {} != false, because {}.toString() returns "[object Object]" --');
console.log(false == {});       // false
console.log({}.toString());     // [object Object]

console.log(!{});               // false

console.log('-- Comparing --');
console.log('-- 1. string will be converted to number or NaN when comparing with a number, and "" will be converted to 0 --');
console.log(12 < "2");          // false
console.log("12" < "2");        // true
console.log("" < 2);            // true

console.log('-- 2. NaN can not be compared with any value, even if NaN itself, always return false --');
console.log(NaN == NaN);        // false

console.log(NaN == null);       // false
console.log(NaN == undefined);  // false
console.log(0 <= NaN);          // false
console.log(0 >= NaN);          // false
console.log(undefined <= NaN);  // false
console.log(undefined >= NaN);  // false
console.log(null <= NaN);       // false
console.log(null >= NaN);       // false

console.log(2 <= "2a");         // false, since "2a" is converted to NaN
console.log(2 >= "2a");         // false, since "2a" is converted to NaN

console.log('-- 3. undefined can only == null and == undefined, and can not do any other comparing even if <= undefined --');
console.log(undefined == null);         // true
console.log(undefined == undefined);    // true

console.log(undefined == "");           // false
console.log(undefined == false);        // false
console.log(undefined <= undefined);    // false
console.log(undefined <= null);         // false
console.log(undefined >= null);         // false
console.log(0 <= undefined);            // false
console.log(0 >= undefined);            // false

console.log('-- 4. null will be converted to "" when <, >, <=, >= comparing --');
console.log(12 <= null);        // false
console.log(12 >= null);        // true
console.log("12" <= null);      // false
console.log("12" >= null);      // true

console.log(0 == null);         // false
console.log("" == null);        // false

console.log('-- 5. object, including {}, [], will be call toString() when comparing --');
console.log(12 < {});           // false, since {}.toString() is "[object Object]", and then converted to NaN
console.log(12 > {});           // false, since {}.toString() is "[object Object]", and then converted to NaN
console.log("[a" < {});         // true, since {}.toString() is "[object Object]"
console.log("[a" > {});         // false, since {}.toString() is "[object Object]"
console.log(12 < []);           // false, since {}.toString() is "", and then converted to 0
console.log(12 > []);           // true, since {}.toString() is "", and then converted to 0
console.log("[a" < []);         // false, since {}.toString() is ""
console.log("[a" > []);         // true, since {}.toString() is ""

console.log('-- 6. According to 4 and 5, we can get below weird result: --');
console.log(null < []);         // false
console.log(null > []);         // false
console.log(null == []);        // false
console.log(null <= []);        // true
console.log(null >= []);        // true

(arr)에서는 JavaScript의 모든 오브젝트가 truthy이기 때문에 arr이 오브젝트라면 항상 true로 평가됩니다.(null은 오브젝트가 아닙니다!)

[] == false이치노음음의 ==후 경우 이 Number로 됩니다.처음에는 오브젝트를 프리미티브로 변환하고, 양쪽이 모두 그렇지 않은 경우 양쪽을 Number로 변환합니다.string(문자열 비교는 양쪽이 문자열인 경우 사용됩니다).는 '[] == false->'' == false->0 == 0->true.

예:

const array = []
const boolValueOfArray = !!array // true

그 이유는

ToNumber(ToPrimitive([])) == ToNumber(false)  
  1. [] 있다Array→ "오브젝트" →ToPrimitive([]) " →→ " →ToNumber("")0
  2. ToNumber(false)→ → 0
  3. 0 == 0 → 참

요소가(0,은 '0, false'로 됩니다.true Equality 사용==.

1. [] == false; // true, because an empty array has nothing to be truthy about
2. [2] == false; // false because it has at least 1 item
3. [false] == false; // also false because false is still an item
4. [[]] == false; // false, empty array is still an item

, 비교 ===변수의 내용 및 데이터 유형을 평가하려고 합니다. 그 이유는 다음과 같습니다.

1. [] === false; // false, because an array (regardless of empty or not) is not strictly comparable to boolean `false`
2. [] === true; // false, same as above, cannot strictly compare [] to boolean `true`
3. [[]] === false; // true, because see #1

도 왜 것 같다.(arr && arr == false)trueMDN에 가입되어 있는 오퍼레이터의 우선 순위를 잘 모르는 분들을 위해== 우선도가 &&는 「」입니다.(arr && (arr == false))와일드카드의 답변 뒤에는true && true★★★★★★★★★★★★★★★★★★.true.

var arr = [];
if (arr && arr == false) console.log("...what??");

는 JavaScript 어레이를 하여 새 수 .list = [] 현재 되고 있는 배열의 합니다.list.length = 0.

출처: JavaScript어레이

knocko.js 매핑플러그인을 사용하려고 해도 위의 어느 것도 도움이 되지 않았습니다.아마 빈 어레이는 비어 있지 않기 때문입니다.

결국 다음을 사용하게 되었다:data-bind="if: arr().length"그게 효과가 있었어

이것은 OP의 질문이 아니라 녹아웃에 특유한 것이지만, 비슷한 상황에서 다른 누군가가 여기를 브라우징하는 데 도움이 될 수도 있다.

언급URL : https://stackoverflow.com/questions/5491605/empty-arrays-seem-to-equal-true-and-false-at-the-same-time

반응형