const title = document.querySelector('#title') as Element;
if(title !== null){
title.innerHTML = 'some txt';
}
if(title instanceof Element){
title.innerHTML = 'some txt';
}
if(title?.innerHTML){
title.innerHTML = 'some txt'
}
let link = document.querySelector('.link');
if(link instanceof HTMLAnchorElement){
link.href = 'http://kakao.com';
}
let btn = document.querySelector('#button')
if(btn instanceof HTMLButtonElement){
btn.addEventListener('click',function(){
console.log('hello world');
})
}
const img = document.querySelector('#image')
if(img instanceof HTMLImageElement){
img.src = 'new.jpg'
}
const naverLink = document.querySelectorAll('.naver')
naverLink.forEach(
(link) => {
if(link instanceof HTMLAnchorElement){
link.href = 'https://kakao.com'
}
})
for(let i = 0; i < 3; i++){
let a = naverLink[i];
if(a instanceof HTMLAnchorElement){
a.href = 'https://kakao.com'
}
}
class 문법을 사용할 때 type 지정
1.
class Anchester {
name:string;
constructor(name:string){
this.name = name;
}
myFunc(a:string){
console.log('안녕'+a);
}
}
const person = new Anchester('Myung-seong');
2.
class Car {
model:string;
price:number;
constructor(carModel:string,carPrice:number){
this.model = carModel,
this.price = carPrice
}
tax():number{
return this.price / 10
}
}
const sonata = new Car('Sonata',26000000)
console.log(sonata);
console.log(sonata.tax());
3.
class seperateWord {
stringArray: string[]
numberArray: number[]
constructor(...wordChunk:(number|string)[]){
this.stringArray = [];
this.numberArray = [];
[...wordChunk].forEach((word)=>{
if(typeof word === 'string'){
this.stringArray.push(word)
}else if(typeof word === 'number'){
this.numberArray.push(word)
}
})
}
}
const chunks = new seperateWord('132','하하하',2400,'안녕',130,777)
chunks.stringArray
chunks.numberArray