function makeObj(name){
var obj = {
name : name ,
todo : "학교에 갓습니다",
setName : function (name){
this.name = name;
return this;
},
getName : function(){
console.log(this.name);
},
toString : function(){
return this.name + this.todo;
}
};
return obj;
};
var v1 = makeObj("태경");
var v2 = makeObj("지훈");
console.log(v1.toString());
console.log(v2.toString());
function GoSchool(name,todo,toString){
this.name = name;
this.todo = todo;
};
GoSchool.prototype.toString = function(){return this.name + this.todo;};
//생성자 함수는 new 키워드로 생성한다.
//new 키워드를 사용하지 않으면 this 는 기본 적으로 window 객체를 가르키키때문에 윈도우 객체에 속성이 추가된다.
//예)
function GoSchool2(name){
this.name = name;
};
var v4 = GoSchool2("이것은 윈도우");
console.log(window.name);
//결과 이것은 윈도우 가 출력됨.
//캡슐화
//가정 위 GoSchool 객체에 숫자가 입력되면 안되는 경우가 있다면
//get set 메소드를 통해 입력값을 제한할수 있다.
GoSchool.prototype.toString = function (){
alert(typeof(this.name));
if(typeof(this.name) === "string" && typeof(this.todo) === "string" ){
alert(this.name);
alert(this.todo);
return this.name + this.todo;
}else{
alert(this.name);
alert(this.todo);
return "입력이 바르지 않음";
};
};
var v3 = new GoSchool("형주","학교에 갓습니다");
var v5 = new GoSchool(1,"학교에 갓습니다1");
console.log(v3.toString());
console.log(v5.toString());
//생성자를 이용할시 공통적으로 사용되는 함수 는 protoTyped 으로 따로 정의한다
//protoType 이란 객체마다 가지게되는 공통적인 기능을 따로 모아 하나로 관리하는 객체를 말한다.
//객체를 생성할때마다 동일한 함수객체가 생성되는것을 막기 위함.
//상속 : 속성은 다르고 동일한 기능이 있을경우 상속 을 하여 코드를 간략화한다.
//GoSchool 을 상속하는 GoHome 만들기
//좀 함축적인과정이 있어서 언뜻 보기에 이해가 잘안하지만
//상속받을 생성자를 만들고 기존 생성자에 파라미터는 그대로 넣어준다
//내부 변수에 상속받을 생성자를 초기화 하고 그 변수에 파라미터로 받은것을 다시 인자로 넣어서 호출한다.
//추가할 속성을 정의한다.
function GoHome(name,todo,time){
this.aaa = GoSchool;
this.aaa(name,todo);
this.time = time;
};
GoHome.prototype = GoSchool.prototype;
GoHome.prototype.constructor = GoSchool;
var v6 = new GoHome("태경이가","집에갓습니다.");
console.log(v6.toString());
댓글 없음:
댓글 쓰기