현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.
객체
(object) 생성자(constructor)는 객체 레퍼(wrapper)를 생성한다.
문법
// Object initialiser or literal
{ [ nameValuePair1[, nameValuePair2[, ...nameValuePairN] ] ] }
// Called as a constructor
new Object([value])
파라미터
nameValuePair1, nameValuePair2, ... nameValuePairN
- 이름(문자열)이 값(아무 값)과 콜론(colon)으로 구분되어있는 이름과 값의 쌍들.
- value
- 아무 값.
설명
객체 생성자는 입력받은 값으로부터 객체 래퍼를 생성한다. 만약 값이 null
이거나 undefined
이면 빈 객체가 반환되고, 그렇지 않은 경우 입력받은 값에 따라 강제 형변환된 객체가 반환된다.
생성자가 아닌 맥락에서 불리면, Object
는 new Object()
와 동일하게 동작한다.
object initializer / literal syntax 참조.
객체 생성자의 속성
Object.length
- 값은 1.
Object.prototype
- 객체 타입의 모든 객체에 속성(property)들을 추가할 수 있게 함.
객체 생성자의 메소드
Object.assign()
- 하나 혹은 그 이상의 소스(source) 객체들로부터 모든 열거가능한 속성들을 대상(target) 객체에 복사하여 새 객체를 생성.
Object.create()
- 명시된 프로토타입(prototype) 객체와 속성들을 포함하는 새 객체를 생성.
Object.defineProperty()
- 주어진 기술자(descriptor)에 의해 기술된(described) 이름붙은 속성을 객체에 추가.
Object.defineProperties()
- 주어진 기술자들에 의해 기술된 이름붙은 속성들을 객체에 추가.
Object.freeze()
- 객체를 프리징(freeze)함: 다른 코드로 어느 속성도 지우거나 변경할 수 없다.
Object.getOwnPropertyDescriptor()
- 객체의 이름붙은 속성의 기술자를 반환.
Object.getOwnPropertyNames()
- 주어진 객체 자신의 열거가능하거나 열거불가능한 속성들의 이름을 포함하는 배열(array)를 반환.
Object.getOwnPropertySymbols()
- 주어진 객체의 모든 심볼 속성을 배열로 반환.
Object.getPrototypeOf()
- 명시된 객체의 프로토타입을 반환.
Object.is()
- 두 값을 구분할 수 있는지(즉, 같은지) 비교.
Object.isExtensible()
- 객체의 확장이 가능한지 확인.
Object.isFrozen()
- 객체가 프리징 되었는지 확인.
Object.isSealed()
- 객체가 실링(seal) 되었는지 확인.
Object.keys()
- 주어진 객체 자신의 열거가능한 속성들의 이름 포함하는 배열을 반환.
Object.observe()
- 비동기적으로(Asynchronously) 객체의 변화를 감지.
Object.getNotifier()
- Get a notifier with which to create object changes manually.
Object.preventExtensions()
- 객체의 어떤 확장도 방지.
Object.seal()
- 다른 코드가 객체의 속성을 삭제하지 못하도록 방지.
Object.setPrototypeOf()
- 프로토타입(즉, 내부의
[[Prototype]] 속성)
을 설정
객체 인스턴스와 객체의 프로토타입 객체
모든 자바스크립트의 객체들은 Object
로부터 유래한다; 모든 객체들은 Object.prototype
로부터 메소드들과 속성들을 상속받고, 그것들은 덮어씌워질 수 있다. 예를 들어, 다른 생성자의 프로토타입이 constructor
속성과 자신의 toString()
메소드를 덮어쓴다. Object
의 프로토타입 객체의 변경은 그 변경에 영향을 받는 속성과 메소드가 덮어씌워지지 않은 이상 포로토타입 체인(chain)을 따라 모든 객체들로 전달된다.
속성
Object.prototype.constructor
- Specifies the function that creates an object's prototype.
Object.prototype.__proto__
- Points to the object which was used as prototype when the object was instantiated.
Object.prototype.__noSuchMethod__
- Allows a function to be defined that will be executed when an undefined object member is called as a method.
Object.prototype.__count__
Used to return the number of enumerable properties directly on a user-defined object, but has been removed.Object.prototype.__parent__
Used to point to an object's context, but has been removed.
메소드
Object.prototype.__defineGetter__()
- Associates a function with a property that, when accessed, executes that function and returns its return value.
Object.prototype.__defineSetter__()
- Associates a function with a property that, when set, executes that function which modifies the property.
Object.prototype.__lookupGetter__()
- Returns the function associated with the specified property by the
__defineGetter__
method. Object.prototype.__lookupSetter__()
- Returns the function associated with the specified property by the
__defineSetter__
method. Object.prototype.hasOwnProperty()
- Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
Object.prototype.isPrototypeOf()
- Returns a boolean indication whether the specified object is in the prototype chain of the object this method is called upon.
Object.prototype.propertyIsEnumerable()
- Returns a boolean indicating if the internal ECMAScript DontEnum attribute is set.
Object.prototype.toSource()
- Returns string containing the source of an object literal representing the object that this method is called upon; you can use this value to create a new object.
Object.prototype.toLocaleString()
- Calls
toString()
. Object.prototype.toString()
- Returns a string representation of the object.
Object.prototype.unwatch()
- Removes a watchpoint from a property of the object.
Object.prototype.valueOf()
- Returns the primitive value of the specified object.
Object.prototype.watch()
- Adds a watchpoint to a property of the object.
Object.prototype.eval()
Used to evaluate a string of JavaScript code in the context of the specified object, but has been removed.
예제
undefined 와 null 타입이 주어진 객체 사용
다음 예제는 변수 o에 비어있는 객체를 저장한다 :
var o = new Object();
var o = new Object(undefined);
var o = new Object(null);
Boolean 객체를 생성하기 위한 객체 사용
다음 예제들은 변수 o에 Boolean
객체들을 저장한다 :
// equivalent to o = new Boolean(true); var o = new Object(true);
// equivalent to o = new Boolean(false); var o = new Object(Boolean());
명세서
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition. | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) The definition of 'Object' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object' in that specification. |
Standard |
브라우저 호환성
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |