답변:
key of obj
이것은 JavaScript의 key in obj. (커피 스크립트를 사용하여 of키를 참조 할 때, 그리고 in어레이의 값을 참조 할 때 : val in arr여부를 테스트한다 val이다 arr.)
객체의 프로토 타입을 무시하려면 thejh의 대답이 맞습니다. null또는 undefined값으로 키를 무시하려면 Jimmy의 대답이 맞습니다 .
'?' 운영자는 존재 여부를 확인합니다.
if obj?
# object is not undefined or null
if obj.key?
# obj.key is not undefined or null
# call function if it exists
obj.funcKey?()
# chain existence checks, returns undefined if failure at any level
grandChildVal = obj.key?.childKey?.grandChildKey
# chain existence checks with function, returns undefined if failure at any level
grandChildVal = obj.key?.childKey?().grandChildKey
null.
obj.key?아마도 가장 간결 할 것입니다.
obj.hasOwnProperty(name)
(상속 된 속성 무시)
key of obj값이 문자열이나 숫자이면 오류가 발생 하기 때문에이 응답이 마음에 듭니다. Cannot use 'in' operator to search. 이 경우 개체가 정의되지 않고 null이 아닌 경우 작동합니다.
own key of objaditionally 테스트에도 작동합니다.hasOwnProperty(). "가장 가능성이 높은"것은 내가 시도하지 않은 것에서 비롯된 것이지만이 구문은 이해력이 뛰어납니다.