📚apply(), call(), bind()

Function.prototype.~

let func1 = () => {
    console.log(this);
}

func1();

console.log('end');
let obj = {
    test:()=>{
        return this;
    }
    , test1:function(){
        return this;
    }
}

obj.test() == obj; // false
obj.test1() == obj; // true

í•Ļėˆ˜ę°€ í˜ļėķœë˜ëŠ” ë°Đė‹ė— 따띾 this가 닮띾ė§‘니ë‹Ī.

Function.prototype.apply()

function introduce(a, b) {
    console.log('ė•ˆë…• ë‚īėīëĶ„ė€ '
        + this.name
        + 'ęģ  나ėī는 '
        + this.age
        + 'ė‚ī ėīė•ž ėž˜ëķ€íƒí•ī');

    console.log(a);
    console.log(b);
}

var person = {
    name: 'ëŊžėĢž'
    , age: 20
}

introduce.apply(person, [123, 234])
// ė•ˆë…• ë‚īėīëĶ„ė€ ëŊžėĢžęģ  나ėī는 20ė‚ī ėīė•ž ėž˜ëķ€íƒí•ī
// 123
// 234
  • introduce() ė—ęēŒ this가 person ėž„ė„ 멅ė‹œ

  • ėļėˆ˜ëŠ” ë°°ė—ī

  • 바로 í•Ļėˆ˜ ė‹Ī

Funtion.prototype.call()

function introduce(a, b) {
    console.log('ė•ˆë…• ë‚īėīëĶ„ė€ ' 
        + this.name 
        + 'ęģ  나ėī는 ' 
        + this.age 
        + 'ė‚ī ėīė•ž ėž˜ëķ€íƒí•ī');
    console.log(a);
    console.log(b);
}
var person = {
    name: 'ëŊžėĢž'
    , age: 20
}
introduce.call(person, 123, 234) 
// ė•ˆë…• ë‚īėīëĶ„ė€ ëŊžėĢžęģ  나ėī는 20ė‚ī ėīė•ž ėž˜ëķ€íƒí•ī 
// 123
// 234
  • introduce() ė—ęēŒ this가 person ėž„ė„ 멅ė‹œ

  • ėļėˆ˜ëŠ” ėžë°˜ė ėœž

  • 바로 í•Ļėˆ˜ė‹Ī

Funtion.prototype.bind()

function introduce(a, b) {
    console.log('ė•ˆë…• ë‚īėīëĶ„ė€ '
        + this.name
        + 'ęģ  나ėī는 '
        + this.age
        + 'ė‚ī ėīė•ž ėž˜ëķ€íƒí•ī');

    console.log(a);
    console.log(b);
}

var person = {
    name: 'ëŊžėĢž'
    , age: 20
}

let func1 = introduce.bind(person, 123, 234);
func1();
// ė•ˆë…• ë‚īėīëĶ„ė€ ëŊžėĢžęģ  나ėī는 20ė‚ī ėīė•ž ėž˜ëķ€íƒí•ī
// 123
// 234
  • ėƒˆëĄœėšī í•Ļėˆ˜ëĄœ ė„ļ팅í•īė„œ ëĶŽí„ī

  • 바로 ė‹Ī행ė•ˆëĻ.

Last updated