๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง

๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง 2022.04.28.๋ชฉ

by DevIseo 2022. 5. 2.

Today I Learn 220428

<setAttribute()์™€ classList.add()์˜ ์ฐจ์ด>

setAttribute()๋Š” ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ

  • setAttribute๋Š” ๋„ˆ๋ฌด ๊ฐ•๋ ฅํ•ด์„œ ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ.
  • ์ด๋Š” inline์œผ๋กœ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ผ CSS ํŒŒ์ผ์—์„œ ์ ์šฉํ•œ ์„ค์ •๋ณด๋‹ค ์šฐ์„ ์œผ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค!!

๋”ฐ๋ผ์„œ,

  1. ์ •ํ™•ํžˆ๋Š” setAttribute()์™€ classList๊ฐ€ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š” ๋Œ€์ƒ์˜ ๋ฒ”์œ„๊ฐ€ ๋‹ค๋ฆ„
#setAttribute()๋Š” HTML ์š”์†Œ์— inline ์Šคํƒ€์ผ๋กœ ์–ด๋– ํ•œ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ
-class ์†์„ฑ
-style ์†์„ฑ
-href ์†์„ฑ
-....๋“ฑ๋“ฑ
#classList()๋Š” ์š”์†Œ์˜ ์—ฌ๋Ÿฌ ์†์„ฑ ์ค‘ ํ•˜๋‚˜์ธ ํด๋ž˜์Šค ์†์„ฑ๋งŒ์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ
  1. ์š”์†Œ์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€/๋ณ€๊ฒฝ/์ œ๊ฑฐ ํ•˜๋Š”์‹์œผ๋กœ ์š”์†Œ๋ฅผ ๋™์ ์œผ๋กœ ๋‹ค๋ฃจ๊ณ  ์‹ถ๋‹ค๋ฉด, classList๋ฅผ ์ถ”์ฒœ!

์‚ฌ์‹ค ์‹ค๋ฌด์—์„œ๋Š” ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค!

 

this - "์ž๊ธฐ์ฐธ์กฐ๋ณ€์ˆ˜"

์ •์˜

  • ๋‚ด๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ถˆ๋ฆฌ๋Š๋ƒ์— ๋”ฐ๋ผ ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ๊ฒƒ.

๋ชฉ์ 

  • "๊ฐ์ฒด"์—์„œ "๋‚ด"๊ฐ€ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด

"์–ด๋””์„œ" "์–ด๋–ป๊ฒŒ" ๋ถ€๋ฅด๋Š๋ƒ์— ๋”ฐ๋ผ this๋Š” ๋‹ฌ๋ผ์ง„๋‹ค!

  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ ๋˜์—ˆ์„ ๋•Œ -> ์ „์—ญ ๊ฐ์ฒด
    • ํ•จ์ˆ˜() -> window
    function test(){
        console.log(this)
    }
    test()
    Window {0: global, window: Window, self: Window, document: document, name: '', location: Locationโ€ฆ}
  • ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœ ๋˜์—ˆ์„ ๋•Œ -> **"๊ทธ" ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด์œ ํ•œ ๊ฐ์ฒด **
    • ๊ฐ์ฒด.๋ฉ”์„œ๋“œ() ->๊ฐ์ฒด
    const obj = {}
    
    const obj = {
        test : function(){
            console.log(this)
        }
    }
    
    obj.test()
{test: ฦ’}
	test: ฦ’ ()
		arguments: null
		caller: null
		length: 0
		name: "test"
		prototype: {constructor: ฦ’}
		[[FunctionLocation]]: VM4936:2
		[[Prototype]]: ฦ’ ()
		[[Scopes]]: Scopes[2]
	[[Prototype]]: Object
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ ๋˜์—ˆ์„ ๋•Œ -> "๋ฏธ๋ž˜"์— ์ƒ์„ฑ๋  "์ธ์Šคํ„ด์Šค" -> ๊ฐ์ฒด
    Teacher {name: 'iseo'}
    	name: "iseo"
    	[[Prototype]]: Object
    		constructor: ฦ’ Teacher(name)
    		[[Prototype]]: Object
    //cf.
    const person2 = Teacher('iseo')
    //์ด๋ ‡๊ฒŒ ๋„ฃ์œผ๋ฉด(newํ‚ค์›Œ๋“œ ๋นผ๋จน์œผ๋ฉด) ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ๊ณผ ๊ฐ™์•„์ ธ ์ „์—ญ์ธ window์— Teacher : 'iseo'๊ฐ€ ๋“ค์–ด๊ฐ€๋ฒ„๋ฆผ....
    //๊ฒฐ๋ก  : ์ด ๊ฒฝ์šฐ์˜ this๋Š” window์— ๋ฐ”์ธ๋”ฉ ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค!!
  • function Teacher(name){
        this.name = name
        //"๋ฏธ๋ž˜"์— ์ƒ์„ฑ๋  "์ธ์Šคํ„ด์Šค"
    }
    
    const person = new Teacher('iseo')
    
    person
  • apply/call/bind๋กœ "๊ฐ„์ ‘"ํ˜ธ์ถœ ๋˜์—ˆ์„ ๋•Œ

์ฐธ๊ณ ํ•˜๊ธฐ ์ข‹์€ ๋ธ”๋กœ๊ทธ ๊ธ€ ๋งํฌ

๋Œ“๊ธ€