티스토리 뷰
# 함수의 종류
1 선언적함수
-선언적함수는 선언된 위치와 상관없이 어디서든 호출할 수 있다.
ex)
<html>
<body>
<script>
document.write(a());
function a(){ return 100; }
</script>
</body>
</html>
2 내부함수
-내부함수는 선언된 함수내에서만 호출가능하다.
ex)
<html>
<body>
<script>
function a(){
function b(){ return 100; }
return 200;
}
document.write(a());
document.write("<br/>");
document.write(b()); // 에러~
</script>
</body>
</html>
3 무명함수
-이름없는 함수
<html>
<body>
<script>
function a(){
return function(){ return 100; };
}
//document.write(a()());
</script>
</body>
</html>
# 객체
<html>
<body>
<script>
function Dog(s){
this.name=s; // 자바스크립트 평션의 멤버변수선언방법
this.changeName=function(str){ // 자바스크립트 평션의 메서드선언방법
this.name=str;
}
}
var a=new Dog("뚱이");
var b=new Dog("아치");
document.write(a.name);
document.write("<br/>");
document.write(b.name);
document.write("<br/>");
a.changeName("행운이");
document.write(a.name);
</script>
</body>
</html>
# String내장객체 (JAVA랑 완전똑같다 JAVA중급 똑바로 했으면
걍 넘어가라 근데 중요하니까 한번더 봐도된다)
<html>
<body>
<script>
var str="aABbefgefs";
//01234567891011
var n0=str.charAt(2); // 인덱스숫자에 있는 문자 읽어오기
document.write(n0+"<br/>");
var n1=str.charCodeAt(0); // 인덱스숫자에 있는 문자 유니코드로 읽어오기
document.write(n1+"<br/>");
var n2=str.substring(2,5); // 2<= <5 까지 내용 읽어오기
document.write(n2+"<br/>");
var n3=str.toUpperCase(); // 대문자로 바꾸는
document.write(n3+"<br/>");
var n4=str.toLowerCase(); // 소문자로 바꾸는
document.write(n4+"<br/>");
var n5=str.indexOf("ef"); // b에해당하는 인덱스 순서 읽어오기 (없으면 -1 출력)
document.write(n5+"<br/>");
var str2="2017/5/18";
var n6=str2.split("/"); // "/" 기준으로 나누기(배열로 나눠짐)
document.write(n6+"<br/>");
var str3=" abc ";
document.write(str3.length); // 5
var n7=str3.trim(); // 공백지워라
document.write(n7.length); // 3
document.write(str3.length); // 5 다시 출력해도 다시5 즉,String객체는 내용불변
</script>
</body>
</html>
'VIEW > JAVASCRIPT' 카테고리의 다른 글
[javascript] 이벤트처리,window,document,innerHTML (0) | 2017.12.01 |
---|---|
[javascript]객체생성방법,for in,date객체,Array객체 (0) | 2017.12.01 |
[javascript] input type,자료형,주석문,배열,내장함수 (0) | 2017.12.01 |
[javascript] script 기본문법 (0) | 2017.12.01 |
[javascript] html,css 기본문법 (0) | 2017.12.01 |