- Console object는 debugging console을 사용할 수 있는 권한을 제공한다.
- Console Object Method
assert() | assertion이 false일때 console에 error 메시지를 뿌려준다./ assertion이 true일 경우 무시 |
Syntax
console.assert(assertion, obj1[,obj2,...,objN]);
console.assert(assertion, msg[,subst1,...substN]); // C-like message formattting
assertion
어떤 boolean 식. 만일 assertiong이 false일 경우, message를 console로 내보낸다.
obj1 ... objN
출력할 Javascritp object의 리스트. 이러한 object의 각각의 string 표현이 목록에 있는 순서대로 덧붙여 출력된다.
msg
하나도 없거나 또는 더 많은 치환 문자열을 포함하는 Javascritp string
ubst1 ... substN
msg안에 치환문자열에 대응하는 Javascript object들. 이 파라미터를 사용하면 출력포멧을 다양하게 해 준다.
예제)
const errorMsg = 'the # is not even';
for (let number = 2; number <= 5; number += 1) {
console.log('the # is ' + number);
console.assert(number % 2 === 0, {number: number, errorMsg: errorMsg});
// or, using ES2015 object property shorthand:
// console.assert(number % 2 === 0, {number, errorMsg});
}
// output:
// the # is 2
// the # is 3
// Assertion failed: {number: 3, errorMsg: "the # is not even"}
// the # is 4
// the # is 5
// Assertion failed: {number: 5, errorMsg: "the # is not even"}
다음은 Node 에서나 브라우저에서 치환문자열을 포함한 string이 console.log에서 파라미터로써 사용할 수 있다.
console.log('the word is %s', 'foo');
// output: the word is foo
그러나, 어떤 브라우저에서는 치환문자열을 가진 string이 console.assert에서 의도한 대로 작동이 안된다.
console.assert(false, 'the word is %s', 'foo');
// correct output in Node.js and some browsers
// (e.g. Firefox v60.0.2):
// Assertion failed: the word is foo
// incorrect output in some browsers
// (e.g. Chrome v67.0.3396.87):
// Assertion failed: the word is %s foo
clear() | console에 있는 모든 메시지를 지운다. |
Syntax
console.clear()
count() | count에 대한 특정한 호출이 호출된 횟수를 log한다. |
Syntax
console.count([label]);
label
string으로 제공된다며, 이러한 label을 가지고 호출된 횟수를 출력한다.
만일 미제공된다고, "default" label을 가진것으로 호출된것으로 처리된다.
예제)
let user = "";
function greet() {
console.count();
return "hi " + user;
}
user = "bob";
greet();
user = "alice";
greet();
greet();
console.count();
///////Console 출력//////////////////////
//"default: 1"
//"default: 2"
//"default: 3"
//"default: 4"
let user = "";
function greet() {
console.count(user);
return "hi " + user;
}
user = "bob";
greet();
user = "alice";
greet();
greet();
console.count("alice");
/////////////////////Console 출력/////////////////////////////////////
// "bob: 1"
// "alice: 1"
// "alice: 2"
// "alice: 3"
위의 예처럼 label에 따라서 count를 서로 다른게 유지할 수 있다.
error() | error 메시지를 consol에 출력한다. |
Syntax
console.error(obj1[,obj2,...,objN]);
console.error(msg [,subst1,...,substN]);
console.excption(obj1[, obj2...,objN]);
console.excpetion(msg[,subst1,...,substN]);
console.exception()은 console.error()의 별칠으로 기능적으로 동일하다.
parameters
obj1...objN : 출력할 javascript의 object 리스트, 작성된 순서로 이러한 object의 string 표현이 덧붙여 출력된다.
msg : 없거나 많은 치환 string을 포함한 Javascriipt string
subst1...sutstN : msg 안에 치환문자열을 교체하기 위한 Javascript object. 이것을 이용하면 출력 포멧을 다양하게 할 수 있다.
from)
www.w3schools.com/jsref/obj_console.asp
developer.mozilla.org/en-US/docs/Web/API/console/assert#syntax
'개발?! > JAVASCRIPT' 카테고리의 다른 글
Date Types (0) | 2023.01.09 |
---|---|
javascript 프로그래밍 레퍼런스 사이트 (0) | 2020.12.14 |