리터럴 생성자
이 경고 범주의 철자는 [literal-constructors]
입니다.
함수를 생성자로 사용하지 마십시오.
무슨 일이 일어났나요?
리터럴 구조 함수를 생성자로 사용했습니다.
이것이 왜 나쁜가요?
Number
같은 리터럴 구조 함수를 일반 함수로 호출하면 전달된 값이 원시 숫자로 강제로 변환됩니다. 그러나 Number
을 생성자로 호출하면 해당 값이 포함된 Number
에서 파생된 객체가 반환됩니다. 이는 낭비이며 예상한 결과가 아닐 가능성이 높습니다. 또한 반환되는 값이 기본값이 아니기 때문에 예기치 않거나 혼란스러운 동작이 발생할 수 있습니다.
예제
import QtQuick Item { function numberify(x) { return new Number(x) } Component.onCompleted: { let n = numberify("1") console.log(typeof n) // object console.log(n === 1) // false if (new Boolean(false)) // All objects are truthy! console.log("aaa") // aa } }
이 경고를 해결하려면 이러한 함수를 생성자로 호출하지 말고 일반 함수로 호출하세요:
import QtQuick Item { function numberify(x) { return Number(x) } Component.onCompleted: { let n = numberify("1") console.log(typeof n) // number console.log(n === 1) // true if (Boolean(false)) console.log("aaa") // <not executed> } }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.