객체 생성자 함수(★)
내장 객체를 생성할 때는 이미 자바스크립트 엔진에 내장되어 있는 객체 생성자 함수를 사용하여 객체를 생성합니다.
function 함수명(매개변수1, 매개변수2,......) this 속성명 = 새 값; this 함수명 = function(){ //자바스크립트 실행 } } let 참조 변수(인스턴스 네임) = new 함수명(); //객체생성
var 참조 변수 = { 속성 : 새값, 함수명 : function() { ... } }
객체 생성자 표기법 (함수와 객체가 합쳐진 식)
//객체 생성자 표기법(함수와 객체가 합쳐진 식)
function obj5(a,b){
this.a = a;
this.b = b;
this.c = function(){
return a * b;
}
}
let result1 = new obj5(100,200);
document.write(result1.a);
//100
//객체 생성자 표기법(함수와 객체가 합쳐진 식)
function obj5(a,b){
this.a = a;
this.b = b;
this.c = function(){
return a * b;
}
}
let result1 = new obj5(100,200);
let result2 = new obj5("자바스크립트를", "실행했습니다.");
document.write(result1.a);
document.write(result1.b);
document.write(result1.c());
document.write("<br>");
document.write(result2.a);
document.write(result2.b);
//100
//200
//20000
//자바스크립트를실행했습니다.
샘플1
function CheckWeight (name, height, weight){
this.userName = name;
this.userHeight = height;
this.userWeight = weight;
this.minWeight;
this.maxWeight;
this.getInfo = function(){
let str = "";
str += "이름: "+this.userName+", ";
str += "키: "+this.userHeight+", ";
str += "몸무게: "+this.userWeight+", ";
return str;
}
this.getResult = function(){
this.minWeight = (this.userHeight - 100) * 0.9 - 5;
this.maxWeight = (this.userHeight - 100) * 0.9 + 5;
if( this.userWeight > this.minWeight && this.userWeight <= this.maxWeight){
return "정상 몸무게입니다.";
} else if ( this.userWeight < this.minWeight ){
return "살좀 찌세요.";
}else {
return "살좀 빼세요.";
}
}
}
let kim = new CheckWeight("소라", 168, 50);
let han = new CheckWeight("정순", 156, 50);
document.write(kim.getInfo());
document.write(han.getResult());
//이름: 소라, 키: 168, 몸무게: 50, 정상 몸무게입니다.
복습예제
//객체생성자 함수
function func6(num1, str1, str2){
this.youNum = num1;
this.youStr1 = str1;
this.youStr2 = str2;
this.result = function(){
//let str = this.youNum + this.youStr1 + this.youStr2;
//return str;//출력문
let str = "";
str += this.youNum +". ";
str += this.youStr1 +"가 ";
str += this.youStr2 + "되었습니다.<br>";
return str;
}
}
let java = new func6(6, "함수","실행");
document.write(java.result());
//객체 생성자 함수(함수+인스턴스 객체(매개변수)) 객체 생성자를 만들때 앞글자는 대문자!
function You3(name,job){
this.name = name;
this.job = job;
this.study = function(){
document.write("내 이름은" + this.name + "이며","직업은" + this.job + "입니다.<br>");
}
}
let char1 = new You3("웹쓰", "웹 퍼블리셔");
let char2 = new You3("웹스토리보이", "프론트앤드 개발자");
char1.study();
char2.study();
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery</title>
</head>
<body>
<!-- script -->
<script src="jquery.min_1.12.4.js"></script>
<script>
document.write("내이름은 웹쓰이며, 직업은 웹 퍼블리셔 입니다.<br>");
document.write("내이름은 도하은이며, 내일 자기소개를 할겁니다.<br><br>");
//매개변수가 함수로 출력
function func1(name, job){
document.write("내 이름은"+name+"이며","직업은"+job+"입니다.<br>");
}
func1("웹쓰","웹퍼블리셔");
func1("도하은","자기소개");
document.write("<br>");
//변수 선언하고 함수 출력
function func2(name,job){
document.write("내 이름은" + name + "이며","직업은" + job + "입니다.<br>");
}
let youName1 = "웹쓰";
let youJob1 = "웹퍼블리셔";
let youName2 = "도하은";
let youJob2 = "자기소개";
func2(youName1, youJob1);
func2(youName2, youJob2);
document.write("<br>");
//객체를 선언하고 함수로 출력
function func3(name,job){
document.write("내 이름은" + name + "이며","직업은" + job + "입니다.<br>");
}
const you = [
{
name:"웹쓰",
job:"웹퍼블리셔"
},
{
name: "웹스토리보이",
job: "프론트앤드 개발자"
}
];
func3(you[0].name, you[0].job);
func3(you[1].name, you[1].job);
//객체 + 메서드
const you2 = {
name1:"웹쓰",
job1: "웹퍼블리셔",
name2: "웹스토리보이",
job2: "프론트앤드 개발자",
study1: function(){
document.write("내 이름은" + this.name1 + "이며","직업은" + this.job1 + "입니다.<br>");
},
study2: function(){
document.write("내 이름은" + this.name2 + "이며","직업은" + this.job2 + "입니다.<br>");
}
}
you2.study1();
you2.study2();
//객체 생성자 함수(함수+인스턴스 객체(매개변수)) 객체 생성자를 만들때 앞글자는 대문자!
function You3(name,job){
this.name = name;
this.job = job;
this.study = function(){
document.write("내 이름은" + this.name + "이며","직업은" + this.job + "입니다.<br>");
}
}
let char1 = new You3("웹쓰", "웹 퍼블리셔");
let char2 = new You3("웹스토리보이", "프론트앤드 개발자");
char1.study();
char2.study();
//프로토타입 메서드
function You4(name, job){
this.name = name;
this.job = job;
}
You4.prototype.study = function(){
document.write("내 이름은" + this.name + "이며","직업은" + this.job + "입니다.<br>");
}
let char3 = new You4("웹쓰", "웹 퍼블리셔");
let char4 = new You4("웹스토리보이", "프론트앤드 개발자");
char3.study();
char4.study();
//객체 리터럴
function You5(name,job){
this.name = name;
this.job = job;
}
You5.prototype = {
study1: function(){
document.write("내 이름은" + this.name + "이며","직업은" + this.job + "입니다.<br>");
},
study2 : function(){
document.write("내 이름은" + this.name + "이며","직업은" + this.job + "입니다.<br>");
}
}
let char5 = new You5("웹쓰", "웹 퍼블리셔");
let char6 = new You5("웹스토리보이", "프론트앤드 개발자");
char5.study();
char6.study();
</script>
</body>
</html>
Last updated
Was this helpful?