본문 바로가기

반응형

전체 글

(645)
javascript : 상속 function Rectangle(w, h) { var width = w; var height = h; this.getWidth = function () { return width; }; this.getHeight = function () { return height; }; this.setWidth = function (w) { if (w < 0) { throw '길이는 음수일 수 없습니다.'; } else { width = w; } }; this.setHeight = function (h) { if (h < 0) { throw '길이는 음수일 수 없습니다.'; } else { height = h; } }; } Rectangle.prototype.getArea = function () { return t..
javascript : new 키워드 생성자 함수를 생성할 때는 모두 new 키워드를 사용해왔습니다. 왜 new 키워드를 사용할까요? new 키워드를 사용하지 않으면 어떠한 일이 발생할까요? 생성자 함수를 생성하고 new 키워드로 객체를 생성합니다. // 생성자 함수를 선언합니다. function Constructor(value){ this.value = value; } // 변수를 선언합니다. var constructor = new Constructor('Hello'); // 출력합니다. console.log(constructor.value); Rectangle 객체와 함수 // 생성자 함수를 선언합니다. function Rectangle(width, height){ this.width = width; this.height = height..
javascript : 프로토타입으로 메서드 생성 function Student(name, korean, math, english, science){ this.이름 = name; this.국어 = korean; this.수학 = math; this.영어 = english; this.과학 = science; } Student.prototype.getSum = function(){}; Student.prototype.getAvarage = function(){}; Student.prototype.gettoString = function(){};
javascript : instanceof instanceof는 해당 객체가 어떠한 생성자 함수로 생성됐는지 확인할 때 instanceof 키워드를 사용합니다. student 객체는 Student 생성자 함수의 인스턴스이므로 코드 7-6의 첫 번째 경고창에만 true가 표시됩니다. // 생성자 함수를 선언합니다. function Student(name, korean, math, english, science) { this.이름 = name; this.국어 = korean; this.수학 = math; this.영어 = english; this.과학 = science; } // 인스턴스를 생성합니다 var student = new Student('홍정민',96,98,92,98); // 출력합니다. console.log(student instance..
javascript : 메서드 생성 기본적인 메서드 생성과 함수 설정 function Student(name, korean, math, english, science) { this.이름 = name; this.국어 = korean; this.수학 = math; this.영어 = english; this.과학 = science; this.getSum = function () { return this.국어 + this.수학 + this.영어 + this.과학; }; this.getAverage = function () { return this.getSum() / 4; }; this.toString = function () { return this.이름 + '\t' + this.getSum() + '\t' + this.getAverage(); ..
javascript : 배열 복제 for를 사용한 배열복제 // 배열을 선언합니다. var orignalArray = [1, 2, 3, 4, 5]; // 배열을 복제합니다. var newArray = []; for(var i =0 ;i< originalArray.length;i++){ newArray[i] = orignalArray[i]; } 전개 연산자를 사용한 배열 복제 // 배열을 선언합니다. const originalArray = [1,2,3,4,5]; // 배열을 복제합니다. const newArray = [...originalArray]; 배열 복제 확인 // 배열을 선언합니다. const originalArray = [1,2,3,4,5]; // 배열을 복제합니다. const newArray = [...originalArray..
javascript : 값 복사 , 깊은 복사 값 복사 : 깊은 복사 // 변수를 선언합니다. var originalValue = 10; var newValue = originalValue; // 원본 값을 변경합니다. originalValue = 273; // 출력합니다 console.log(originalValue); console.log(newValue); 273 10 참조 복사 : 얕은 복사 // 변수를 선언합니다. var originalArray = [1,2,3,4]; var newArray = originalArray; // 원본 배열의 값을 변경합니다. originalArray[0] = 273; // 출력합니다. console.log(originalArray); console.log(newArray); [ 273, 2, 3, 4 ] [ ..
javascript : option 객체 function test(options) { // 옵션 객체를 초기화합니다. options.valueA = options.valueA || 10; options.valueB = options.valueB || 20; options.valueC = options.valueC || 30; // 출력합니다. console.log(options.valueA + ':' + options.valueB + ':' + options.valueC); } test({ valueA: 52, valueC: 273 }); 52:20:273 ||

반응형