The toString() 은 문자열을 반환하는 object의 대표적인 방법이다
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
obj.toString()
Description
모든 객체에는 객체가 텍스트 값으로 표시되거나 객체가 문자열이 예상되는 방식으로 참조 될 때 자동으로 호출되는 toString() 메서드가 있습니다. 기본적으로 toString() 메서드는 Object에서 비롯된 모든 객체에 상속됩니다. 이 메서드가 사용자 지정 개체에서 재정의되지 않으면 toString()은 "[object type]"을 반환합니다. 여기서 type은 object type입니다. 다음 코드는 이것을 설명합니다
var o = new Object(); o.toString(); // returns [object Object]
Note: 자바스크립트 1.8.5부터 null의 toString()을 호출하는 경우 [object Null]을 반환하며, undefined는 [object Undefined]를 반환합니다. 이는 ECMAScript 제 5판과 후속 정오표에 정의되어 있습니다. See toString으로_객체_클래스_검사.
Examples
기본 toString 메소드 재정의
기본 toString() 메서드 대신에 호출될 함수를 정의할 수 있습니다. toString() 메서드는 인자를 취하지 않고 문자열을 반환합니다. 직접 생성한 toString() 메서드는 원하는 어떤 값이든 될 수 있지만 해당 객체에 대한 정보를 전달하고 있을 때 가장 유용할 것입니다.
다음 코드는 Dog 객체 타입을 정의하고 Dog 타입을 따르는 theDog를 생성합니다:
function Dog(name, breed, color, sex) {
this.name = name;
this.breed = breed;
this.color = color;
this.sex = sex;
}
theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
커스텀 객체의 toString() 메서드를 호출하는 경우 Object로부터 상속받은 기본 값을 반환하게 됩니다:
theDog.toString(); // returns [object Object]
다음 코드는 기본 toString() 메서드를 재정의하는 dogToString()을 생성하고 할당합니다. 이 함수는 객체의 name, breed, color, sex를 포함하는 문자열을 "property = value;"의 형태로 만들어냅니다.
Dog.prototype.toString = function dogToString() {
var ret = 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed;
return ret;
}
앞선 코드를 사용하면 문자열 컨텍스트에서 theDog가 사용될 때마다 자바스크립트는 자동으로 dogToString() 함수를 호출하여 다음 문자열을 반환합니다:
"Dog Gabby is a female chocolate Lab"
toString()으로 객체 클래스 검사
toString()은 모든 객체에 사용되어 해당 객체의 클래스를 가져올 수 있습니다. Object.prototype.toString()을 모든 객체에 사용하기 위해서는 Function.prototype.call() 나 Function.prototype.apply()를 사용해서 검사하고자 하는 객체를 thisArg로 불리는 첫번째 파라미터로 넘겨야 합니다.
var toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] toString.call(Math); // [object Math] // Since JavaScript 1.8.5 toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
| ECMAScript 5.1 (ECMA-262) The definition of 'Object.prototype.toString' in that specification. |
Standard | Call on null returns [object Null], and undefined returns [object Undefined] |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.prototype.toString' in that specification. |
Standard | |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Object.prototype.toString' in that specification. |
Draft |
브라우저 호환성
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support