-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
18장 함수와 일급 객체 #12
18장 함수와 일급 객체 #12
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
방학을 하니... JS와 멀어지고 있네요... 오랜만에 하느라 힘드셨을텐데 고생하셨어요 👍
|
||
## name 프로퍼티 | ||
|
||
`name` 프로퍼티는 함수의 이름을 나타낸다. `name` 프로퍼티는 `ES5`와 `ES6`에서 동작을 달리한다. 익명 함수 표현식의 경우 `ES5`에서 `name` 프로퍼티는 빈 문자열을 값으로 갖는다. 하지만 `ES6`에서는 함수 객체를 가리키는 식별자를 값으로 갖는다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
### prototype 프로퍼티가 있는 경우 | ||
|
||
```js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class
문법과 연관지어 어떻게 사용되는지 나오면 좋을 것 같은데, 그건 나중에 클래스에서 나오겠죠?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생 많았습니다! 좀 더 읽고 코멘트 더 추가할게요
## 일급 객체의 조건 | ||
|
||
1. 무명의 리터럴로 생성할 수 있다. 즉, 런타임에 생성이 가능하다. | ||
2. 변수나 자료구조에 저장할 수 있다. | ||
3. 함수의 매개변수에 전달할 수 있다. | ||
4. 함수의 반환값으로 사용할 수 있다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
프로그래밍 언어에서 일급 시민(first class citizen)이란
일급 시민의 조건은 3가지 이다
- 변수에 담을 수 있다.
- 인자로 전달할 수 있다.
- 반환값으로 전달할 수 있다.
그리고 객체가 일급 시민으로 취급되면 일급 객체, 함수가 일급 시민이면 일급 함수라고 부른다고 합니다
즉, 일급 시민이나 일급 객체나 일급 함수나 모두 다 같은 이야기!
1960년대에 개발된 함수형 프로그래밍 언어인 LISP에서 이 개념이 유래되었다고 하네요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
함수에 인자로 넘기기, 수정하기, 변수에 대입하기와 같은 연산을 지원할 때 일급 객체라고 한다네요
그러면 js의 함수가 아닌 다른 객체들도 일급 객체일까요?
배열, json들도 함수에 인자로 넘기기, 수정하기, 변수 대입, 런타임 생성 가능 하므로 일급 객체라고 볼 수 있을 거 같아요.
참고자료 : https://ko.wikipedia.org/wiki/%EC%9D%BC%EA%B8%89_%EA%B0%9D%EC%B2%B4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러면 다른 언어들에서도 객체는 일급 객체라 볼 수 있을 거 같은데,
C나 JAVA에선 함수는 일급 객체가 아니지만, JS에세는 함수가 일급 객체로 취급되는 것이 차이점인 것 같아요.
그 이유가 궁금했는데,
C에서는 모든 함수가 컴파일 타임에 생성되고, 런타임에 생성이 불가능하고,
함수의 매개변수에 콜백으로 전달하더라도 클로저를 지원하지 않는 문제가 있네요
JAVA에서는 함수가 따로 존재하지 않고, method 형태로 항상 객체에 종속되어 존재합니다.
또 C와 마찬가지로 런타임에 새로운 메서드 생성이 불가능하다고 하네요 (그런데 의존성 주입과 헷갈려요! 준필이가 잘 설명해 줄 듯..)
그리고 JAVA 8 미만 버전에선 람다와 메서드 참조가 없어서 함수를 변수처럼 저장하거나 전달할 수도 없었다고 하네요!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일급 객체를 지원하지 않는 언어에서도 의존성 주입이 불가능하진 않기 때문에(ex. C) 직접적인 관련이 있다고 보기는 어려울 것 같아요. 하지만 일급 객체를 지원하는 언어에서는 의존성을 훨씬 쉽게 전달하고 교체할 수 있을테니 DI를 구현하기가 더 쉽고 자연스럽다고 생각할 수 있을듯 합니다!
`arguments`객체는 인수를 프로퍼티 값으로 소유하며 프로퍼티 키는 인수의 순서를 나타낸다. `arguments` 객체의 `callee`프로퍼티는 호출되어 `arguments` 객체를 생성한 함수, 즉 함수 자신을 가리키고 `arguments` 객체의 `length`프로퍼티는 인수의 개수를 가리킨다. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caller
callee
를 처음 들어봐 신기해서 열심히 찾다보니 갑자기 현타가 오더라구요... 이제는 지원도 안 되고 작동도 안 된다고 합니다
그냥 심심하신 분만 읽어봅시다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
역시 온갖 것이 다 있지만 다 안 쓰는 JS..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 관심이 없으면 지나쳐도 된다 문장 보자마자 지나쳤는데 .. ㅋㅋㅋㅋ(온갖 것이 다 있는 js) 열심히 찾아보셧꾼요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수고하셨습니다아~~
|
||
 | ||
|
||
여기서 복습! 프로퍼티 어트리뷰트에서 `writable`은 `value`의 변경여부, `configurable`은 프로퍼티 변경(삭제 및 수정) 여부를 다룬다! 자세한 내용 16장을 다시 보자. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
복습까지 남겨주는 성종짱.. 친절해요...
|
||
`length` 프로퍼티는 함수를 정의할 때 선언한 매개변수의 개수를 가리킨다. | ||
|
||
이때 `length` 프로퍼티와 `argument` 객체의 `length` 프로퍼티는 다른 값을 가질 수 있으므로 주의바란다! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
실제로 전달된 것의 개수와 선언된 개수의 차이군요....
모든 객체는 `Prototye`이라는 내부 슬롯을 갖는다. `Prototype` 내부 슬롯은 객체지향 프로그래밍의 상속을 구현하는 프로토타입 객체를 가리킨다. 프로토타입 객체에 대해서는 다음장에서 자세히 살펴보자. | ||
|
||
‘**proto**’ 프로퍼티는 `Prototype` 내부 슬롯이 가리키는 프로토타입 객체에 접근하기 위해 사용하는 접근자 프로퍼티다. 내부 슬롯에는 직접 접근할 수 없고 간접적인 접근 방법(접근자 프로퍼티)을 제공하는 경우에만 접근 가능하다. | ||
|
||
## prototype 프로퍼티 | ||
|
||
`prototpye` 프로퍼티는 생성자 함수로 호출할 수 있는 함수 객체, 즉 `constructor`만이 소유하는 프로퍼티다. 일반 객체와 생성자 함수로 호출할 수 없는 `non-constructor`에는 `protorype` 프로퍼티가 없다. | ||
|
||
`prototpye` 프로퍼티는 함수가 객체를 생성하는 생성자 함수로 호출될 때 생성자 함수가 생성할 인스턴스의 프로토타입의 객체를 가리킨다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ㅋㅋㅋㅋㅋㅋㅋ여기 prototype 오타가ㅠㅠㅠ수도 없이 많아요 웃고 갑니다
## 일급 객체의 조건 | ||
|
||
1. 무명의 리터럴로 생성할 수 있다. 즉, 런타임에 생성이 가능하다. | ||
2. 변수나 자료구조에 저장할 수 있다. | ||
3. 함수의 매개변수에 전달할 수 있다. | ||
4. 함수의 반환값으로 사용할 수 있다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
함수에 인자로 넘기기, 수정하기, 변수에 대입하기와 같은 연산을 지원할 때 일급 객체라고 한다네요
그러면 js의 함수가 아닌 다른 객체들도 일급 객체일까요?
배열, json들도 함수에 인자로 넘기기, 수정하기, 변수 대입, 런타임 생성 가능 하므로 일급 객체라고 볼 수 있을 거 같아요.
참고자료 : https://ko.wikipedia.org/wiki/%EC%9D%BC%EA%B8%89_%EA%B0%9D%EC%B2%B4
## 일급 객체의 조건 | ||
|
||
1. 무명의 리터럴로 생성할 수 있다. 즉, 런타임에 생성이 가능하다. | ||
2. 변수나 자료구조에 저장할 수 있다. | ||
3. 함수의 매개변수에 전달할 수 있다. | ||
4. 함수의 반환값으로 사용할 수 있다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러면 다른 언어들에서도 객체는 일급 객체라 볼 수 있을 거 같은데,
C나 JAVA에선 함수는 일급 객체가 아니지만, JS에세는 함수가 일급 객체로 취급되는 것이 차이점인 것 같아요.
그 이유가 궁금했는데,
C에서는 모든 함수가 컴파일 타임에 생성되고, 런타임에 생성이 불가능하고,
함수의 매개변수에 콜백으로 전달하더라도 클로저를 지원하지 않는 문제가 있네요
JAVA에서는 함수가 따로 존재하지 않고, method 형태로 항상 객체에 종속되어 존재합니다.
또 C와 마찬가지로 런타임에 새로운 메서드 생성이 불가능하다고 하네요 (그런데 의존성 주입과 헷갈려요! 준필이가 잘 설명해 줄 듯..)
그리고 JAVA 8 미만 버전에선 람다와 메서드 참조가 없어서 함수를 변수처럼 저장하거나 전달할 수도 없었다고 하네요!!
`arguments`객체는 인수를 프로퍼티 값으로 소유하며 프로퍼티 키는 인수의 순서를 나타낸다. `arguments` 객체의 `callee`프로퍼티는 호출되어 `arguments` 객체를 생성한 함수, 즉 함수 자신을 가리키고 `arguments` 객체의 `length`프로퍼티는 인수의 개수를 가리킨다. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
역시 온갖 것이 다 있지만 다 안 쓰는 JS..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨습니다 깔끔한 내용정리 굿굿
## 일급 객체의 조건 | ||
|
||
1. 무명의 리터럴로 생성할 수 있다. 즉, 런타임에 생성이 가능하다. | ||
2. 변수나 자료구조에 저장할 수 있다. | ||
3. 함수의 매개변수에 전달할 수 있다. | ||
4. 함수의 반환값으로 사용할 수 있다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일급 객체를 지원하지 않는 언어에서도 의존성 주입이 불가능하진 않기 때문에(ex. C) 직접적인 관련이 있다고 보기는 어려울 것 같아요. 하지만 일급 객체를 지원하는 언어에서는 의존성을 훨씬 쉽게 전달하고 교체할 수 있을테니 DI를 구현하기가 더 쉽고 자연스럽다고 생각할 수 있을듯 합니다!
|
||
따라서 `forEach, filter, map` 와 같은 배열 메서드들을 사용하면 에러가 발생한다. 배열 매서드를 사용하려면 `Function.prototype.call, Function.prototype.apply`를 사용해 간접 호출해야 한다. 이 이상은 다음장에서… | ||
|
||
때문에 이러한 번거로움을 해결하기 위해 ES6에서는 `Rest 파라미터`를 도입했다(준필님이 예전에 말씀해주신 그것). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest 파라미터가 채고
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수고하셨슴다~~~~👍👍👍👍🎅🎅
`arguments`객체는 인수를 프로퍼티 값으로 소유하며 프로퍼티 키는 인수의 순서를 나타낸다. `arguments` 객체의 `callee`프로퍼티는 호출되어 `arguments` 객체를 생성한 함수, 즉 함수 자신을 가리키고 `arguments` 객체의 `length`프로퍼티는 인수의 개수를 가리킨다. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 관심이 없으면 지나쳐도 된다 문장 보자마자 지나쳤는데 .. ㅋㅋㅋㅋ(온갖 것이 다 있는 js) 열심히 찾아보셧꾼요
|
||
### **1. `arguments` 및 `caller`의 차이** | ||
|
||
- **화살표 함수**는 `arguments`와 `caller` 프로퍼티를 가지지 않습니다. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arguments 대신 Rest 파라미터 (...args)를 사용하면 된다고 합니당
const arrowFunc = (...args) => {
console.log(args); // [1, 2, 3]
};
arrowFunc(1, 2, 3);
재밌게 봐주세용~ 피드백은 언제나 환영입니다ㅎㅎ 😄