Object 객체는 객체의 가장 기본적인 형태를 가지고 있는 객체이다. 다시 말해서 아무것도 상속받지 않는 순수한 객체다. 자바스크립트에서는 값을 저장하는 기본적인 단위로 Object를 사용한다
var grades = {'egoing': 10, 'k8805': 6, 'sorialgi': 80};
//Obect.kets()
var arr = ["a","b","c"]
console.log('Object,keys(arr)',Object.keys(arr));
// "0,1,2"
//Object.prototype.toString()
var o = new Object();
console.log('o.toString()', o.toString());
// "[object Object]"
var a = new Array(1,2,3);
console.log('a.toString()', a.toString());
// "1,2,3"
Object 확장
Object.prototype.contain = function(neddle) {
for(var name in this){ //객체를 의
if(this[name] === neddle){
return true;
}
}
return false;
}
var o = {'name':'egoing', 'city':'seoul'}
console.log(o.contain('egoing')); //true
var a = ['egoing','leezche','grapittie'];
console.log(a.contain('leezche')); //true
Object 객체는 확장하지 않는 것이 바람직하다. 왜냐하면 모든 객체에 영향을 주기 때문이다.
var o = {'name':'egoing', 'city':'seoul'}
var a = ['egoing','leezche','grapittie'];
for(var name in o){
console.log(name);
}
// name
// contain
확장한 프로퍼티인 contain이 포함되어 있다. 객체가 기본적으로 가지고 있을 것으로 예상하고 있는 객체 외에 다른 객체를 가지고 있는 것은 개발자들에게 혼란을 준다.
var o = {'name':'egoing', 'city':'seoul'}
var a = ['egoing','leezche','grapittie'];
for(var name in o){
if(o.hasOwnProperty(name))
console.log(name);
}
// name
// city
hasOwnProperty는 인자로 전달된 속성의 이름이 객체의 속성인지 여부를 판단한다. 만약 prototype으로 상속 받은 객체라면 false가 된다.