-
Notifications
You must be signed in to change notification settings - Fork 7
ํจ์
iforget edited this page May 1, 2020
·
1 revision
- ํจ์ ํธ์ถ์ ํํ์
- return์ ๋ช ์์ ์ผ๋ก ํธ์ถํ์ง ์์ผ๋ฉด ๋ฐํ๊ฐ์ undefined
- ํจ์๋ ๊ฐ์ฒด์ด๊ณ ๋ณ์, ํ๋กํผํฐ, ๋ฐฐ์ด ์์์ ํ ๋นํ ์ ์์
- 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());
- ์?
- ๊ฐ๋ ์ฑ
- ํ ์คํธ
- ์ฌ์ฌ์ฉ์ฑ
- 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!';
}
};
- ๊ฐ์ฒด๋ ๋ฐฐ์ด์ ๋ณ์๋ก 'ํด์ฒด'ํ ์ ์์
- ๋ฐฐ์ด :
//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
- ๊ฐ๊ฒฐํ ๊ตฌ๋ฌธ์ ์ง๋ ์ต๋ช ํจ์
- ์ต๋ช ํจ์๋ฅผ ๋ง๋ค์ด ๋ค๋ฅธ๊ณณ์ ์ ๋ฌํ๋ ค ํ ๋ ๊ฐ์ฅ ์ ์ฉ
- ๋ฌธ๋ฒ
- 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());