javascript 학습 노트 (9) js 대상 디자인 모델

4568 단어
1. 대상 만 들 기
 
  
var person = new Object();
person.name = "RuiLiang";
person.age = 30;
person.job = "Teacher";
person.sayName = function () {
alert(this.name);
};

person.sayName();

2. 공장 모델
단점: 대상 인식 불가
 
  

function createPerson(name,age,job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function () {
alert(this.name);
};
return o;
}

var person1 = createPerson(" ",30," ");
var person2 = createPerson(" ",24," ");

person1.sayName(); //" "
person2.sayName(); //“ ”

3. 구조 함수 모드
단점: 포장 성 부족
 
  
function Person(name,age,job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = sayName;
}
function sayName() {
alert(this.name);
}

var person1 = new Person(" ",30," ");
var person2 = new Person(" ",24," ");
person1.sayName();
person2.sayName();

4. 원형 모드
단점: 모든 속성 이 인 스 턴 스 로 공유 됨
 
  
function Person() {
}

Person.prototype.name = "ALiang";
Person.prototype.age = 30;
Person.prototype.job = "Teacher";
Person.sayName = function () {
alert(this.name);
}

hasOwnProperty () 방법 은 특정한 속성 이 인 스 턴 스 속성 인지 아 닌 지 를 검사 합 니 다. true 로 돌아 가면
person1.hasOwnProperty("name"); //name person 1 속성
in 연산 자: 대상 을 통 해 접근 하 는 속성 이 존재 하 는 지 여부 입 니 다. true 로 돌아 가 는 속성 이 존재 하 든 인 스 턴 스 가 존재 하 든 원형 이 있 든 상 관 없 이
alert("name" in person1); //name 속성 이 존재 하면 true 로 되 돌려 줍 니 다
속성 이 원형 에 있 는 지 대상 에 있 는 지 확인 하 는 방법:
 
  
function hasPrototypeProperty(object,name) {
return !object.hasOwnProperty(name) && (name in object);
}
//
var person = new Person();
alert(hasPrototypeProperty(person,"name")); //true
person.name = "Grey"; // name
alert(hasPrototypeProperty(person,"name")); //false

isPrototypeOf () 방법 은 지정 한 대상 object 1 이 다른 대상 object 2 의 프로 토 타 입 체인 에 존재 하 는 지 판단 하 는 데 사 용 됩 니 다. true 로 돌아 가지 않 으 면 false 로 돌아 갑 니 다.
형식 은 다음 과 같 습 니 다.
object1.isPrototypeOf(object2);
object 1 은 대상 의 인 스 턴 스 입 니 다.
object 2 는 원형 체인 을 검사 할 다른 대상 입 니 다.
프로 토 타 입 체인 은 같은 대상 유형의 서로 다른 인 스 턴 스 간 에 기능 을 공유 할 수 있 습 니 다.
object 2 의 프로 토 타 입 체인 에 object 1 이 포함 되 어 있다 면 isPrototypeOf 방법 은 true 로 돌아 갑 니 다.
object 2 가 대상 이 아니 거나 object 1 이 object 2 의 프로 토 타 입 체인 에 나타 나 지 않 으 면 isPrototype Of 방법 은 false 로 돌아 갑 니 다.
 
  
//
function Person(){
}

Person.prototype = {
constructor : Person,
name : "ALiang",
age : 30,
job : "Teacher",
sayName : function() {
alert(this.name);
}
};

5. 구조 함수 와 원형 혼합 모드
구조 함수 모델 과 원형 모델 의 장점 을 가지 고 속성 은 구조 함수 모델 을 사용 하 며 방법 은 원형 모델 / 이런 모델 로 가장 광범 위 하 게 사용 된다.
 
  
function Person(name,age,job) {
this.name = name;
this.age = age;
this.job = job;
this.friends = ["xuyun","wuxueming"];
}
Person.prototype = {
constructor : Person,
sayName : function() {
alert(this.name);
}
};

var person1 = new Person("ALiang",30,"Teacher");
var person2 = new Person("ZuNan",26,"Teacher");
person1.friends.push("JunJun");
alert(person1.friends); //"xuyun","wuxueming","JunJun"
alert(person2.friends); //"xuyun","wuxueming"

6. 동적 원형 모드
 
  
function Person(name,age,job) {
this.name = name;
this.age = age;
this.job = job;

if (typeof this.sayName != "function"){ // sayName
Person.prototype.sayName = function() { //
alert(this.name);
};
}

7. 기생 구조 함수 모드
8. 온당 한 구조 함수 모드

좋은 웹페이지 즐겨찾기