Object.prototype
속성(property)은 Object
프로토타입(원형) 객체를 나타냅니다.
Property attributes of Object.prototype |
|
---|---|
Writable | no |
Enumerable | no |
Configurable | no |
설명
JavaScript에서 모든 객체는 Object
의 후손입니다. 즉 모든 객체는 Object.prototype
으로부터 메소드 및 속성을 상속합니다, 비록 재정의(override)될 수 있지만 (null
프로토타입인 Object
제외, 가령 Object.create(null)
). 예를 들어, 다른 생성자의 프로토타입은 constructor
속성을 재정의하고 자체 toString()
메소드를 제공합니다. Object
프로토타입 객체의 변화는 프로토타입 체인을 따라 좀 더 재정의되는 그러한 변화의 대상인 속성 및 메소드를 제외하고 모든 객체로 전해집니다.
속성
Object.prototype.constructor
- 객체의 프로토타입을 생성하는 함수를 지정합니다.
Object.prototype.__proto__
- 객체가 초기화될 때 프로토타입으로 사용된 객체를 가리킵니다.
Object.prototype.__noSuchMethod__
정의되지 않은 객체 멤버가 메소드로서 호출될 때 실행되는 함수를 정의하는 데 쓰였지만 제거되었습니다.Object.prototype.__count__
사용자 정의 객체 상에 직접 있는 열거가능 속성의 수를 반환하는 데 쓰였지만 제거되었습니다.Object.prototype.__parent__
객체 문맥을 가리키는 데 쓰였지만 제거되었습니다.
메소드
Object.prototype.__defineGetter__()
- 함수를 속성에 연결합니다, 접근했을 때 그 함수를 실행해 그 결과값을 반환하는.
Object.prototype.__defineSetter__()
- 함수를 속성에 연결합니다, 설정했을 때 그 속성을 수정하는 함수를 실행하는.
Object.prototype.__lookupGetter__()
__defineGetter__()
메소드에 의해 지정된 속성과 관련된 함수를 반환합니다.Object.prototype.__lookupSetter__()
__defineSetter__()
메소드에 의해 지정된 속성과 관련된 함수를 반환합니다.Object.prototype.hasOwnProperty()
- 객체가 지정된 속성을 프로토타입 체인을 통해 상속되지 않은 그 객체의 직접 속성으로 포함하는지를 나타내는 boolean을 반환합니다.
Object.prototype.isPrototypeOf()
- 지정된 객체가 이 메소드가 호출된 객체의 프로토타입 체인 내에 있는지를 나타내는 boolean을 반환합니다.
Object.prototype.propertyIsEnumerable()
- 내부 ECMAScript [[Enumerable]] attribute가 설정된 경우를 나타내는 boolean을 반환합니다.
Object.prototype.toSource()
- 이 메소드가 호출된 객체를 나타내는 객체 리터럴의 출처를 포함하는 문자열을 반환합니다; 새로운 객체를 만들기 위해 이 값을 쓸 수 있습니다.
Object.prototype.toLocaleString()
toString()
을 호출합니다.Object.prototype.toString()
- 객체의 문자열 표현을 반환합니다.
Object.prototype.unwatch()
- 객체 속성에서 감시점을 제거합니다.
Object.prototype.valueOf()
- 지정된 객체의 원시값을 반환합니다.
Object.prototype.watch()
- 객체 속성에 감시점을 추가합니다.
Object.prototype.eval()
지정된 객체의 문맥에서 JavaScript 코드 문자열을 평가하는 데 쓰였지만 제거되었습니다.
예
JavaScript는 엄밀히 말해서 하위클래스(sub-class) 객체가 없기에, prototype은 객체 역할을 하는 특정 함수의 "기반 클래스" 객체를 만드는 유용한 차선책입니다. 예를 들어:
var Person = function() { this.canTalk = true; }; Person.prototype.greet = function() { if (this.canTalk) { console.log('Hi, I am ' + this.name); } }; var Employee = function(name, title) { Person.call(this); this.name = name; this.title = title; }; Employee.prototype = Object.create(Person.prototype); Employee.prototype.constructor = Employee; Employee.prototype.greet = function() { if (this.canTalk) { console.log('Hi, I am ' + this.name + ', the ' + this.title); } }; var Customer = function(name) { Person.call(this); this.name = name; }; Customer.prototype = Object.create(Person.prototype); Customer.prototype.constructor = Customer; var Mime = function(name) { Person.call(this); this.name = name; this.canTalk = false; }; Mime.prototype = Object.create(Person.prototype); Mime.prototype.constructor = Mime; var bob = new Employee('Bob', 'Builder'); var joe = new Customer('Joe'); var rg = new Employee('Red Green', 'Handyman'); var mike = new Customer('Mike'); var mime = new Mime('Mime'); bob.greet(); // Hi, I am Bob, the Builder joe.greet(); // Hi, I am Joe rg.greet(); // Hi, I am Red Green, the Handyman mike.greet(); // Hi, I am Mike mime.greet();
스펙
브라우저 호환성
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) |