- ์ฐ๊ด์๋ ๋ณ์์ ํจ์๋ฅผ ๋ฌถ์ด์ค๋ค.
- prototype์ ์ด์ฉํด ์์์ด ๊ฐ๋ฅํ๋ค.
- ์ด์ Ultra -> Super -> Sub ๋ฅผ ์์๋ฐ๋ ์์์์ ์ฌ์ค Ultra๋ Object๋ฅผ ์์๋ฐ๊ณ ์๋ค.
- Object -> Ultra -> Super -> Sub
- Object์ prototype์ ๋ชจ๋ ๊ฐ์ฒด์ prototype์ด๋ค. ๋ชจ๋ ๊ฐ์ฒด์ ์ํ์ด๋ค.
// Object.keys() ์์ฑ์ ๋ฉ์๋
var arr = ['a','b','c'];
console.log(Object.keys(arr));
//Object.prototype.toString() ํ๋กํ ํ์
๊ฐ์ฒด
var a = new Array(1,2,3);
console.log(a.toSring());1๋ฒ๊ณผ 2๋ฒ์ ์ฌ์ฉ๋ฐฉ๋ฒ์ด ์ ๋ค๋ฅผ๊น? 1๋ฒ Object.keys()๋ ์ธ์๋ฅผ ๋ฐ์์ ์ฌ์ฉํ๋ค.
Object.key(arr) //์ Object๋ ์์ฑ์ ํจ์์ผ ๊ฒ์ด๊ณ
Object.keys = function(){}; // ์ด๋ ๊ฒ ์ ์๋์ด ์์ ๊ฒ์ด๋ค.2๋ฒ Object.prototype.toString()์
Object.prototype.toSring = function(){};์ด๋ ๊ฒ prototype์ผ๋ก ์ ์๋์ด ์์ ๊ฒ์ด๋ค.
๋ฉ์๋๊ฐ ํ๋กํ ํ์ ์ด๋ผ๋ ์๋ฏธ๋ new Object();๋ผ๊ณ ์คํ์ ํ๋ ์๊ฐ์ ์ด๋ ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ Object.prototype ์ด๋ผ๋ ํน์ํ ํ๋กํผํฐ์ ์ ์ฅ๋์ด ์๋ ๊ฐ์ฒด๋ฅผ ์ํ์ผ๋ก ํ๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค. ๊ทธ๋ ๊ฒ ์์ฑ๋ ๊ฐ์ฒด๋ toString์ด๋ผ๋ ๋ฉ์๋๋ฅผ ์ธ ์ ์๋ค.
์ด ๋ ์ฐจ์ด๋ฅผ ์ดํดํด์ผ ํ๋ค.
Object.prototype.contain = function(needle){
for(var name in this){
if(this[name] === needle){
return true;
}
}
return false;
}
var o = {name: 'sla', 'city':'seoul'};
console.log(o.contain('sla'));Object.prototype.contain = function(needle){
for(var name in this){
if(this[name] === needle){
return true;
}
}
return false;
}
var o = {name: 'sla', 'city':'seoul'};
//console.log(o.contain('sla'));
for(var name in o){
console.log(o[name]);
}
//๊ฒฐ๊ณผ๋
/*
sla
seoul
ฦ (needle){
for(var name in this){
if(this[name] === needle){
return true;
}
}
return false;
}
*/์ด๋ ๊ฒ contain ๊น์ง ๊ฐ์ด ๋์จ๋ค. ๋ชจ๋ ๊ฐ์ฒด์ ์ํฅ์ ์ค๊ฑฐ๋ค.
Object.prototype.contain = function(needle){
for(var name in this){
if(this[name] === needle){
return true;
}
}
return false;
}
var o = {name: 'sla', 'city':'seoul'};
//console.log(o.contain('sla'));
for(var name in o){
console.log(name);
}
//๊ฒฐ๊ณผ
//name
//city
//contain contain ์ด ํฌํจ๋์ด ์๋ค.
object์ ํ๋กํผํฐ์ ์ถ๊ฐํ๋ค๋๊ฑด ํธ๋ฆฌํ๋ฉด์๋ ์ํํ๋ค.
Object.prototype.contain = function(needle){
for(var name in this){
if(this[name] === needle){
return true;
}
}
return false;
}
var o = {name: 'sla', 'city':'seoul'};
//console.log(o.contain('sla'));
for(var name in o){
if(o.hasOwnProperty(name)){
console.log(name);
}
} ์์ ์ property์ธ์ง ์ฒดํฌํด์ค๋ค. ๋ถ๋ชจ๋ก ๋ถํฐ ์์๋ฐ์ ๊ฒ์ ์ ์ธ๋๋ค.