참고 : 이 글은 http://blog.gloridea.net/1165540616#comment3445356 에 대한 답변성 글입니다. : )
arguments가 deprecated 되었다고 하셔서 깜짝 놀랐네요 -ㅅ-; 그럴리가! 라고 ㅋㅋ;
찾아보니, Function.prototype.arguments 라는 프로퍼티가 deprecated 된 것이더군요. (함수 내부 스코프 체인에 추가되는 arguments 객체 자체는 계속 사용할 수 있습니다.) 이런 형태는 다음과 같이 사용합니다.
function plus(x, y) {
console.log(plus.arguments)
return x + y;
}
plus(1, 2); 위 코드를 돌려보시면
function plus(x, y) {
console.log(arguments)
return x + y;
}이 코드와 동일하게 동작합니다. Function.prototype.arguments를 사용할 경우, function literal 표현에서는 자신의 arguments를 구하기가 매우 난감해지겠죠. 위와 같이 named function이야 자기 이름 적어주면 되지만... 그렇지 않다면 다음과 같이 해야 할겁니다. 외부에 반드시 자신의 레퍼런스가 있어야 하죠. 그것도 lexical scope에...
var plus = function(x, y) {
console.log(plus.arguments);
return x + y;
}또 하나의 문제. 그런데 이걸 객체의 프로퍼티로 등록할 때는 어떻게 할 수 있을까요?
var MyNumber = $Class({
$init: function(x) {
this.value = x;
}
plus: function(y) {
console.log(this.plus.arguments);
return this.value + y;
}
})
이렇게 하면 됩니다만...call/apply 등과 엮이면 꽤 귀찮아집니다.
var MyOtherNumber = $Class({
$init: function(x) {
this.value = x;
}
});
var num2 = new MyOtherNumber(1);
MyNumber.prototype.plus.call(num2, 2);
// "TypeError: this.plus is undefined" will be raised
function이 독립적인 객체로서 call/apply 등을 통해 다른 객체의 스코프에서 사용될 수 있음을 감안하면
Function.prototype.arguments 때문에 코드의 자유도가 현저히 떨어집니다.
이러한 이유로 아마 Function.prototype.arguments 에서는 제거되고 arguments가 function 내부에서 자동으로
scope chain 내에 포함되는 형태로 바뀐 것으로 보여집니다. 아마 꽤 옛날 일일듯...
'IT' 카테고리의 다른 글
| Is arguments deprecated? (2) | 2009/02/05 |
|---|---|
| IE에서 Array.prototype.slice를 이용한 배열 복사의 문제 (2) | 2009/01/28 |
| IE apply 메서드의 특징 (2) | 2009/01/27 |
| 티스토리용 Syntax Highlight Helper (4) | 2009/01/24 |
| 티스토리에 SyntaxHighlighter 마크업 자동 적용 북마클릿 (2) | 2008/08/18 |
| 원노트, 빠른 편집과 블로깅 (0) | 2007/12/28 |
syntax_highlight_finder.js