본문 바로가기

반응형

언어/javascript

(55)
javascript : String var stringFromLiteral = 'Hello World...!'; var stringFromConstructor = new String('Hello World..!'); var output = ''; output += typeof (stringFromLiteral) + '\n'; output += typeof (stringFromConstructor); console.log(output); string object String 객체는 length 속성을 갖습니다. 이외에도 정말많은 메소드들을 가지고 있습니다.. 필요한건 그때마다 검색해서 사용하는게 좋을듯 합니다. 함수를 사용할 때 주의할점은 기존 변수.메서드 이용시 반환하는 값이 생성되게되는데 이값이 자동적으로 할다되는것은 아닙니다. 예를 들..
javascript : Nmuber 객체 Number 객체는 자바스크립트에서 가장 단순한 객체로 숫자를 표현할 때 사용합니다. 아래와 같이 두가지 방법으로 생성할 수 있습니다. var NumberFromLiteral = 273; var NumberFromConstructor = new Number(273); console.log(NumberFromLiteral); console.log(NumberFromConstructor); 273 [Number: 273] var number = 273.5210332; var output = ''; output += number.toFixed(1) +'\n'; output += number.toFixed(4); console.log(output);​ 메서드 이름 설명 toExponential() 숫자를 지수 ..
javascript : Object 객체 내장함수 Objejct 객체에 있는 메서드를 정리하면 다음과 같습니다. 메서드 이름 설명 constructor() 객체의 생성자 함수를 나타냅니다. hasOwnProperty(name) 객체가 name 속성이 있는지 확인합니다. isPrototypeof(object) 객체가 object의 프로토타입인지 검사합니다 propertyIsEnumerable(name) 반복문으로 열거할 수 있는지 확인합니다. toLocaleString() 객체를 호스트 환경에 맞는 언어의 문자열로 바꿉니다. toString() 객체를 문자열로 바꿉니다. valueOf() 객체의 값을 나타냅니다. var obj = { property : 273}; var output =''; output += "HOP('property') :"+ obj...
javascript : 기본자료형 var primitiveNumber = 273; var objectNumber = new Number(273); var output = ''; output += typeof(primitiveNumber) + ' : ' + primitiveNumber +'\n'; output += typeof (objectNumber) + ' : ' + objectNumber; console.log(output); number : 273 object : 273 기본 자료형과 객체는 자료형이 분명 다릅니다. 하지만 두가지 모두 값을 출력합니다. var primitiveNumber = 273; primitiveNumber.method = function(){ return 'Primitive Method'; }; var out..
javascirpt : 클래스 선언과 속성 javascript에서 class를 선언하고 생성자를 선언하는 방식은 constructor을 이용한다. class Rectangle { constructor(width, height){ this.width = width; this.height = height; } } function Rectangle(width,height){ this.width = width; this.height = height; } 두개의 차이점은 많지가 않다. 메서드 선언에서는 프로타입 안에 선언하는 것이 아니라, class 블록 내부에 선언하게 됩니다. class Rectangle{ constructor(width,height){ this.width = width; this.height = height; } getArea(){ ..
javascript : prototype, constructor Java를 사용해본 사람이라면 Class에 대해서 매우 친숙하다. JavaScirpt도 객체지향 설계를 위해서 이러한 객체들을 prototype이라는 규칙아래에서 사용한다. What is Prototype? 다른 블로그에서 그림을 가져왔다. 일반적으로 function 을 사용해서 객체를 정의하게 되면은 함수와 prototype Object가 동시에 생성된다. 그러면 함수내의 prototype이라는 link를 통해 Prototpye Object에 접근이 가능하다. 따라서 Person.prototpye.eyes = 3 을 사용하게되면 Prototype Objejct에 직접적으로 접근가능한것이다. 이처럼 Prototype Object란 JAVA에서 static 영역처럼 객체가 공유하는 데이터처럼 느껴진다. 그..
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..

반응형