object
Object
Object ๊ฐ์ฒด๋ ๊ฐ์ฒด์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ํํ๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด์ด๋ค. ๋ค์ ๋งํด์ ์๋ฌด๊ฒ๋ ์์๋ฐ์ง ์๋ ์์ํ ๊ฐ์ฒด๋ค.์๋ฐ์คํฌ๋ฆฝํธ์์๋ ๊ฐ์ ์ ์ฅํ๋ ๊ธฐ๋ณธ์ ์ธ ๋จ์๋ก Object๋ฅผ ์ฌ์ฉํ๋ค.
Object -> ๋ชจ๋ ๊ฐ์ฒด์ ๋ถ๋ชจ
Object ๐ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์์ด์ผํ๋ ๊ณตํต๋ ๊ธฐ๋ฅ์ด ์๋ค object์ prototype ๊ฐ์ฒด๋ฅผ ์์ ํ๋๊ฒ์ ํตํด์ ๊ทธ๋ฌํ ๊ธฐ๋ฅ์ ๋ง๋ค ์ ์๋ค.
object ๋ ์์ธํ ์์๋ณด๊ธฐ(์ด๋ ํ ๋ฉ์๋๊ฐ ์๋์ง ํ์ธ ํด๋ณด๊ธฐ ) -> https://developer.mozilla.org/ko/
* object prototype ์ดํฌ๋ง3 ์ด์ฌ์ผ ๋ชจ๋ ๋ธ๋ก์ฐ์ ์์ ์ง์์ด ๋๋ค. object ๋ด์ฅ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ๋๋ ๋ธ๋ผ์ฐ์ ์ง์์ด ์ด๋๊น์ง ๋๋์ง ํ์ธํด๋ด์ผํ๋ค. *
var grades = {'egoing': 10, 'k8805': 6, 'sorialgi': 80};
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
// ๋ง์ฝ์ graphottie ๋ผ๊ณ ํ๋ฉด false๊ฐ ๋์ฌ๊ฒ์ด๋ค ์กด์ฌํ์ง ์๊ธฐ๋๋ฌธ์
var a = ['egoing','leezche','grapittie'];
console.log(a.contain('leezche')); //true
object ํ์ฅ์ํ
๊ทธ๋ฐ๋ฐ Object ๊ฐ์ฒด๋ ํ์ฅํ์ง ์๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค. ์๋ํ๋ฉด ๋ชจ๋ ๊ฐ์ฒด์ ์ํฅ์ ์ฃผ๊ธฐ ๋๋ฌธ์ด๋ค.
ํ์ฅํ ํ๋กํผํฐ์ธ contain์ด ํฌํจ๋์ด ์๋ค. ๊ฐ์ฒด๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ง๊ณ ์์ ๊ฒ์ผ๋ก ์์ํ๊ณ ์๋ ๊ฐ์ฒด ์ธ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒ์ ๊ฐ๋ฐ์๋ค์๊ฒ ํผ๋์ ์ค๋ค. ์ด ๋ฌธ์ ๋ฅผ ํํผํ๊ธฐ ์ํด์๋ ํ๋กํผํฐ์ ํด๋น ๊ฐ์ฒด์ ์์์ธ์ง๋ฅผ ์ฒดํฌํด๋ณผ ์ ์๋ hasOwnProperty๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
for(var name in o){
if(o.hasOwnProperty(name))
console.log(name); //name city
//hasOwnProperty -> ์์ ์ ํ๋กํฌํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋๊ฐ๋ฅผ ํ์ธํด์ค
}
Last updated
Was this helpful?