JavaScript30
第九天要學習的是在網頁開發時 Console 的各種用法
Github 檔案位置:09 - Dev Tools Domination
網頁一開始的樣子如下,有個 List 和許多題目
今天要做的事情是熟悉以下 Console 功能
const dogs = [{ name: 'Snickers', age: 2 }, { name: 'hugo', age: 8 }];
function makeGreen() {
const p = document.querySelector('p');
p.style.color = '#BADA55';
p.style.fontSize = '50px';
}
console.log('Hello world');
console.log('%s', 'Hello world');
console.log('%c OwOwOwOwO', 'font-size:50px; background:red; text-shadow: 10px 10px 0 blue')
console.warn('OwOwO');
console.error('AwAwA');
console.info('qqqqqqqqqqqqqqqq');
const p = document.querySelector('p');
console.assert(p.classList.contains('ouch'), 'That is wrong!');
console.clear();
const p = document.querySelector('p');
console.log(p);
console.dir(p);
console.groupEnd('區域名');
前的 Console 輸出都輸出在新區域中dogs.forEach(dog => {
console.groupCollapsed(`${dog.name}`);
console.log(`This is ${dog.name}`);
console.log(`${dog.name} is ${dog.age} years old`);
console.log(`${dog.name} is ${dog.age * 7} dog years old`);
console.groupEnd(`${dog.name}`);
});
console.count('Wes');
console.count('Wes');
console.count('Steve');
console.count('Steve');
console.count('Wes');
console.count('Steve');
console.count('Wes');
console.count('Steve');
console.count('Steve');
console.count('Steve');
console.count('Steve');
console.count('Steve');
console.time('fetching data');
fetch('https://api.github.com/users/wesbos')
.then(data => data.json())
.then(data => {
console.timeEnd('fetching data');
console.log(data);
});
console.table(dogs);
const dogs = [{ name: 'Snickers', age: 2 }, { name: 'hugo', age: 8 }];
function makeGreen() {
const p = document.querySelector('p');
p.style.color = '#BADA55';
p.style.fontSize = '50px';
}
// Regular
console.log('hello');
// Interpolated
console.log('%s', 'Hello world');
// Styled
console.log('%c OwOwOwOwO', 'font-size:50px; background:red; text-shadow: 10px 10px 0 blue')
// warning!
console.warn('OwOwO');
// Error :|
console.error('AwAwA');
// Info
console.info('qqqqqqqqqqqqqqqq');
// Testing
const p = document.querySelector('p');
console.assert(p.classList.contains('ouch'), 'That is wrong!');
// clearing
// console.clear();
// Viewing DOM Elements
console.log(p);
console.dir(p);
// console.clear();
// Grouping together
dogs.forEach(dog => {
console.groupCollapsed(`${dog.name}`);
console.log(`This is ${dog.name}`);
console.log(`${dog.name} is ${dog.age} years old`);
console.log(`${dog.name} is ${dog.age * 7} dog years old`);
console.groupEnd(`${dog.name}`);
});
// counting
console.count('Wes');
console.count('Wes');
console.count('Steve');
console.count('Steve');
console.count('Wes');
console.count('Steve');
console.count('Wes');
console.count('Steve');
console.count('Steve');
console.count('Steve');
console.count('Steve');
console.count('Steve');
// timing
console.time('fetching data');
fetch('https://api.github.com/users/wesbos')
.then(data => data.json())
.then(data => {
console.timeEnd('fetching data');
console.log(data);
});
console.table(dogs);
以上是第九天的製作紀錄,如有錯誤或不足的地方還請多多指教 >.<
14 Must Know Chrome Dev Tools Tricks - #JavaScript30 9/30
[ Alex 宅幹嘛 ] 深入淺出 Javascript30 快速導覽 | Day 9:Dev Tools Domination
MDN Web Docs