//Object.keys()
var arr = ["a", "b", "c"];
console.log('Object.keys(arr)', Object.keys(arr));
//Object.prototype.toString();
var o = new Object();
console.log('o.toString()', o.toString());
var a = new Array(1,2,3);
console.log('a.toString()', o.toString());
모든 객체가 사용할 수 있는 메서드
사용법: 객체식별자.메서드()
예시) o.toString()
정의:Object.prototype.메서드명 = function(){}
-new Object()
명령어로 생성자를 호출하면 객체가 생성된다. 이떄 이 객체는 Object.prototype 속성에 저장되어 있는 객체를 원형으로 한다. 따라서 이 객체는 당연히 toString()
를 호출할 수 있다. 이떄 toSting()
은 이 객체의 메서드로써 사용이 된다.
그런데 Object는 모든 객체의 부모이다. 따라서 어떤 객체를 생성하든 Object.prototype에 정의되어 있는 메서드를 사용할 수 있다.
생성자 함수는 객체이니 속성을 가지고 있는데, 여기에 prototype이라는 특수한 속성이 있다. 이 prototype(원형)에 정의되어 있는 함수는 이 객체의 자식객체에서 사용할 수 있다. prototype에 정의되어 있지 않은 메서드는 자식객체에서 사용할 수 없다.
for(var name in o) {
console.log(name);
}
for(var name in a) {
console.log(name);
}
// 결과
// name city contain
// 0 1 2 contain
// 확장한 프로퍼티 contain이 포함되어 있다.
Object.prototype.hasOwnProperty()
메서드를 사용한다.for(var name in o)
if (o.hasOwnProperty(name))
console.log(name);
for(var name in a)
if (a.hasOwnProperty(name))
console.log(name);
// 결과
// name city
// 0 1 2
// 확장한 프로퍼티 contain이 포함되지 않았다.