Skip to content

ํ•จ์ˆ˜

iforget edited this page May 1, 2020 · 1 revision

๊ธฐ๋ณธ

์ •์˜ : ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ์‹คํ–‰๋˜๋Š” ๋ฌธ์˜ ์ง‘ํ•ฉ

  • ํ•จ์ˆ˜ ํ˜ธ์ถœ์€ ํ‘œํ˜„์‹
  • return์„ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ˜ํ™˜๊ฐ’์€ undefined
  • ํ•จ์ˆ˜๋„ ๊ฐ์ฒด์ด๊ณ  ๋ณ€์ˆ˜, ํ”„๋กœํผํ‹ฐ, ๋ฐฐ์—ด ์š”์†Œ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ

this ํ‚ค์›Œ๋“œ

  • this๋Š” ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์„ ์–ธํ–ˆ๋Š๋ƒ๊ฐ€ ์•„๋‹ˆ๋ผ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœํ–ˆ๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง
const o = {
    name : 'Wallace',
    speak : function(){
        return `My name is ${this.name}!`
    }
};
 
console.log(o.speak()); //My name is Wallace!
 
const speak = o.speak;
console.log(speak === o.speak); //true
 
console.log(speak()); //My name is undefined!
const o = {
    name: 'Julie',
    greetBackwards: function () {
        function getReverseName() {
            let nameBackwards = '';
            for (let i = this.name.length - 1; i >= 0; i--) {
                nameBackwards += this.name[i];
            }
            return nameBackwards;
        }
 
        return `${getReverseName()} si eman yM`;
    }
};
 
console.log(o.greetBackwards());
  
//--------------------------------------------------
  
const o = {
    name: 'Julie',
    greetBackwards: function () {
        const self = this;
 
        function getReverseName() {
            let nameBackwards = '';
            for (let i = self.name.length - 1; i >= 0; i--) {
                nameBackwards += self.name[i];
            }
            return nameBackwards;
        }
 
        return `${getReverseName()} si eman yM`;
    }
};
 
console.log(o.greetBackwards());

Fure Function(์ˆœ์ˆ˜ ํ•จ์ˆ˜)

์ •์˜ : ์ž…๋ ฅ์ด ๊ฐ™์œผ๋ฉด ๊ฒฐ๊ณผ๋„ ๋ฐ˜๋“œ์‹œ ๊ฐ™๋‹ค / ๋ถ€์ˆ˜ํšจ๊ณผ๊ฐ€ ์—†๋‹ค.

  • ์™œ?
    • ๊ฐ€๋…์„ฑ
    • ํ…Œ์ŠคํŠธ
    • ์žฌ์‚ฌ์šฉ์„ฑ

ES6 New Feature

  • spread operator(ํ™•์‚ฐ or ํŽผ์นจ ์—ฐ์‚ฐ์ž) : "..."
    • ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ์ฒด๋ฅผ ๊ฐœ๋ณ„ ๊ฐ’์œผ๋กœ ๋‚˜๋ˆ ์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•˜๋Š” ์—ฐ์‚ฐ์ž
function myFunction(a, b){
    return a + b;
}
  
//ES5
var data = [1, 4];
var result = myFunction.apply(null, data);
  
//ES6
let data = [1, 4];
let result = myFunction(...data);
let array1 = [2, 3, 4];
let array2 = [1, ...array1, 5, 6, 7];
array2; //[ 1, 2, 3, 4, 5, 6, 7 ]  

- rest parameter : ๊ฐ€๋ณ€์ ์ธ ํ•จ์ˆ˜ ์ธ์ž๋ฅผ ํฌ์ฐฉํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ. ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰ ๋งค๊ฐœ๋ณ€์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค.
```javascript
//ES5
function myFunction(a, b){
    //ํ•จ์ˆ˜์•ˆ์—๋งŒ ์กด์žฌํ•˜๋Š” arguments๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ์„œ ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉ
    var args = Array.prototype.slice.call(arguments, myFunction.length);
    console.log(args);
} 
  
//ES6
function myFunction(a, b, ...args){
    //args๊ฐ€ ๋ฐฐ์—ด๋กœ ๋„˜์–ด์˜ด
    console.log(args);
}
  • parameter default value(๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’)
function f(a, b = "default", c = 3){
    return `${a} - ${b} - ${c}`;
}

f(5,6,7);// 5 - 6 - 7
f(5,6);// 5 - 6 - 3
f(5);// 5 - default - 3
f();// undefined - default - 3

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์— ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€

//ES5
var o = {
    name: 'foo',
    bark: function () {
        return 'Woof!';
    }
};
 
//ES6
const o = {
    name: 'foo',
    bark() {
        return 'Woof!';
    }
};

destructuring assignment(ํ•ด์ฒดํ• ๋‹น)

  • ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์„ ๋ณ€์ˆ˜๋กœ 'ํ•ด์ฒด'ํ•  ์ˆ˜ ์žˆ์Œ
  • ๋ฐฐ์—ด :
//ES5
var myArray = [1, 2, 3];
var a = myArray[0];
var b = myArray[1];
var c = myArray[2];
  
//ES6
let myArray = [1, 2, 3];
let [a, b, c, d] = myArray;
d; //undefined
  
let [a, ,b] = [1, 2, 3];
a; //1
b; //3
  • ๊ฐ์ฒด :
const obj = { b: 2, c: 3, d: 4 };
  
const {a, b, c} = obj;
a; //undefined
b; //2
c; //3
d; //ReferenceError : "d"๋Š” ์ •์˜๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•ด์ฒด
function getSentence({subject, verb, object}){
    return console.log(`${subject} ${verb} ${object}`);
}
 
const o = {
  subject : 'I',
  verb : 'am',
  object : 'a boy',
};
 
getSentence(o);//I am a boy
function getSentence([subject, verb, object]){
    return console.log(`${subject} ${verb} ${object}`);
}
 
const arr = {'I', 'am', 'a boy'};
 
getSentence(arr);//I am a boy

arrow function : =>

  • ๊ฐ„๊ฒฐํ•œ ๊ตฌ๋ฌธ์„ ์ง€๋‹Œ ์ต๋ช…ํ•จ์ˆ˜
  • ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋‹ค๋ฅธ๊ณณ์— ์ „๋‹ฌํ•˜๋ ค ํ• ๋•Œ ๊ฐ€์žฅ ์œ ์šฉ
  • ๋ฌธ๋ฒ•
    • function์„ ์ƒ๋žตํ•ด๋„ ๋จ
    • ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋‹จ ํ•˜๋‚˜๋ฟ์ด๋ฉด ๊ด„ํ˜ธ๋„ ์ƒ๋žต ๊ฐ€๋Šฅ
    • ํ•จ์ˆ˜ ๋ฐ”๋””๊ฐ€ ํ‘œํ˜„์‹ ํ•˜๋‚˜๋ผ๋ฉด ์ค‘๊ด„ํ˜ธ์™€ return๋ฌธ๋„ ์ƒ๋žต ๊ฐ€๋Šฅ
const f1 = function(){ return "hello!";}
const f1 = () => "hello!";  
  
const f2 = function(name) {return `Hello, ${name}!`;}
const f2 = name => `Hello, ${name}!`;
  
const f3 = function(a, b) {return a + b;}
const f3 = (a,b) => a + b;
  • ๊ธฐ์กด ํ•จ์ˆ˜์™€ ์ค‘์š”ํ•œ ์ฐจ์ด์ 
    • this๊ฐ€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •์ ์œผ๋กœ ๋ฌถ์ž„
    • ๊ฐ์ฒด ์ƒ์„ฑ์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค
    • arguments๋ณ€์ˆ˜ ์‚ฌ์šฉ x
const o = {
    name: 'Julie',
    greetBackwards: function () {
        const getReverseName = () => {
            let nameBackwards = '';
            for (let i = this.name.length - 1; i >= 0; i--) {
                nameBackwards += this.name[i];
            }
            return nameBackwards;
        };
 
        return `${getReverseName()} si eman yM`;
    }
};
 
console.log(o.greetBackwards());