var str = "Hello World"; try { // String 객체에는 startWith 메서드가 없으므로 예외 발생 document.write(str.startWith("Hello")); } catch (err) { document.write(err + "<br>"); // TypeError: str.startWith is not a function } // String 객체의 prototype에 startWith메서드를 정의합니다. // 이 함수는 전달받은 매개변수로 시작하는 문자열인지 검사하여 boolean 값을 반환합니다. String.prototype.startWith = function(text) { return this.indexOf(text) == 0 // 자신은 this로 참조합니다. } document.write(str.startWith("Hello")); // true