Object API
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
Object
The Object class represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.
developer.mozilla.org
// Object.메소드와 Object.prototype.메소드의 차이점
// Object.keys()와 Object.prototype.toString()
var arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // ["0", "1", "2"]
console.log(arr.toString()); // a,b,c
사용법이 차이가 난다.
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 확장의 위험
왜냐하면 모든 객체에 영향을 미치기 때문이다. 그것이 안좋은 영향이라면 매우 위험하다.
Object.prototype.contain = function(needle) {
for(var name in this) {
if(this[name] === needle)
return true;
}
return false;
}
var o = {'name':'egoing', 'city':'seoul'}
for(var name in o) {
console.log(name); // name city contain
} // contain 함수도 o 객체의 속성, 메소드이기 때문에
// 해결방법
for(var name in o) {
if(o.hasOwnProperty(name)) // 객체에 직접적으로 정의된 요소인지 체크
console.log(name); // name city
}
'Javascript > 기초공부' 카테고리의 다른 글
[Javascript: 참조] 복제 / 참조 / 함수와 참조 (0) | 2020.03.19 |
---|---|
[Javascript: 데이터 타입] 원시 데이터 타입 / 래퍼 객체(Wrapper object) (0) | 2020.03.19 |
[Javascript: 표준 내장 객체] 표준 내장 객체란? / 배열의 확장 (0) | 2020.03.19 |
[Javascript: 상속(Inheritance)] 상속의 사용법 / prototype (0) | 2020.03.19 |
[Javascript: this] 전역객체 / 함수, 메소드, 생성자와 this / apply와 call / apply와 this (0) | 2020.03.18 |