JAVASCRIPT

object 확장

Made Project 2020. 11. 17. 00:00

어떠한 프로그래밍을 만들 때 원리를 먼저 생각하는 것보다는 어떻게 사용될건지를 어디에 사용될건지를 먼저 생각하는 것을 추천드립니다.

 

 

 

Object.prototype.contain=function(needle){

	for(let name in this) {
 		   if(this[name]=== needle){
	   return true;
     }
   }
   return false
 }




let o = {'name':'egoing', 'city':'seoul'}
console.log(o.contain('egoing'));
let a = ['egoing','leezche','grapittie'];
console.log(a.contain('leezche'))

 

Object 모든 객체를 뜻하고 어떠한 메소드를 갖는다고 하면 어떠한 프로토타입이라는 생성자의

프러퍼티 안에 들어 있는 객체를 변경하면 되는 겁니다.

 

Object.prototype.contain

 

결과는 true true 가나옵니다.

 

여기서 this 는 각 객체를 가르킵니다.

 

Object.prototype.contain=function(needle){

	for(let name in this) {
 		   if(this[name]=== needle){
	   return true;
     }
   }
   return false
 }




let o = {'name':'egoing', 'city':'seoul'}
console.log(o.contain('grapittie'));
let a = ['egoing','leezche','grapittie'];
console.log(a.contain('leezche'))

 

false true가 나옵니다.

 

 

이글은 생활코딩을 보고 참고해서 만들었습니다. "egoing님 감사합니다."