๐Ÿ™ Javascript ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๊ณผ ํ™œ์šฉ_01

1
post-thumbnail
post-custom-banner

0. ๐Ÿ™

// Man ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์ƒ์„ฑ
function Man() {
	this.name = "Anonymous";
	this.gender = "Man";
	this.Run = function () {
		return this.name + " is Running. ";
	}
	this.Sleep = function () {
		return this.name + " is Sleeping. ";
	}
}

// SoccerPlayer ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์ƒ์„ฑ
function SoccerPlayer () {
	this.base = new Man();
	this.name = "Anonymous Soccer Player.";
	this.Run = function () {
		// overriding์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
		return this.base.Run();
	}
	this.Pass = function () {
		return this.name+" is passing to other player!";
	}
};

// SoccerPlayer ์˜ ์›ํ˜•์€ Man ์ด๋‹ค.
// SoccerPlayer ๋กœ ๊ฐ์ฒดํ™”ํ•œ๋‹ค.
SoccerPlayer.prototype = new Man();
var player = new SoccerPlayer();

player.name; //Anonymous Soccer Player. SoccerPlayer ๊ฐ์ฒด์˜ ์ด๋ฆ„์ด ์ถœ๋ ฅ๋œ๋‹ค.
player.gender; // Man ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์„ฑ๋ณ„์ด ์ถœ๋ ฅ๋œ๋‹ค.
player.Run(); //Anonymous is Running. ์›ํ˜•์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
//player.Run() ๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฆ„์ด Anonymous Soccer Player๊ฐ€ ์•„๋‹Œ Anonymous ์ด๋‹ค.
player.Pass(); //Anonymous Soccer Player is passing to other player! 
player.Sleep(); //Anonymous Soccer Player is Sleeping.
//player.Pass();์™€ player.Sleep();์€ ์ž์‹ํด๋ž˜์Šค๋กœ ๊ฐ์ฒดํ™” ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—,
//์ด๋ฆ„์ด Anonymous Soccer Player์ด๋‹ค.
  • * Substring์€ ๋‚ด๊ฐ€ ํ•„์š”๋กœํ•˜๋Š” ๋ถ€๋ถ„์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
String map = superman;
map.substring(5,7);
man์„ ๋ฆฌํ„ดํ•œ๋‹ค. 

1. ๋™์  ํ˜•๋ณ€ํ™˜

  • ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋“ค์ด ๊ทธ๋ ‡๋“ฏ ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ์—ญ์‹œ ๊ฐ’์— ๋”ฐ๋ผ ๋ณ€์ˆ˜์˜ ํ˜•๋ณ€ํ™˜์ด ์ž๋™์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

2. ๋ฐฐ์—ด

  • var arr = {'hello', 'javascript', 'world'};
    console.log(arr[0]);
    console.log(arr[1]);
    console.log(arr[2]);

  • var obj = {
    firstname : "ji hoon",
    lastname : "lee",
    age : 28
    };
    console.log(obj.firstname);
    console.log(obj.lastname);
    console.log(obj.age);

  • var person = {
    firstname : "ji hoon",
    lastname : "lee",
    height : 189,
    fullName : function() {
    return firstname + lastname;
    }
    };
    console.log(fullName);

3. Javascript with web(html)

  • ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋จ๊ณผ ๋™์‹œ์— ์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ฐ€ ๋กœ๋“œ๋œ๋‹ค.
  • document.getElementById('demo')์ฝ”๋“œ๋กœ p ํƒœ๊ทธ์— ๋Œ€ํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
  • pํƒœ๊ทธ ๋‚ด๋ถ€๋ฅผ "Hello javascript World"๋กœ ๋Œ€์ฒดํ•œ๋‹ค.
<p id="demo"> This is P tag </p>

<script type="text/javascript">
	document.getElementById('demo').innerHTML = "Hello javascript World";
</script>

4. Javascript function (ํ•จ์ˆ˜)

  • javascript๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
<button type="button" onclick="_func(22)">button</button>
<script type="text/javascript">
function myFunc(a, b) {
  return a+b;
}
document.getElementById("demo").innerHTML = myFunc(5, 4);
</script>
<p id="demo"></p>
<button type="button" onclick="_func(22)">button</button>
<script type="text/javascript">
function _func(num) {
  document.getElementById("demo").innerHTML = num*2;
}
</script>

5. Javascript์˜ ๋ณ€์ˆ˜

  • var๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค. ( var์€ ๋ชจ๋“  ์ž๋ฃŒํ˜•์„ ๋‚ดํฌํ•˜๊ณ  ์žˆ๋‹ค. int, double, string, Object, boolean ๋“ฑ )
  • ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•œ๋‹ค.
var str1 = 'Hello';
var str2 = 'World';
console.log(str1+' '+str2);
document.getElementById("type").innerHTML =
  typeof "Parker" + "<br>" +
  typeof 123 + "<br>" +
  typeof {num:1, str:"hello"} + "<br>" +
  typeof 123.456 + "<br>" +
  typeof [1,2,3] + "<br>"

=> RESULT
string
number
object
number
object

6. [Javascript] ๋‚ ์งœ ํ•จ์ˆ˜

var date = new Date();
var date2 = new Date(99, 5,24,11,33,20,0);
var date3 = new Date("October 12, 2018 12:11:22");
console.log(date);
console.log(date2);
console.log(date3);

date.getDay(); // ์š”์ผ์„ ์ˆซ์ž๋กœ ๋ฐ›์•„์˜จ๋‹ค.
date.getMonth()+1; // ์›”์„ ๋ฐ›์•„์˜จ๋‹ค.(0๋ถ€ํ„ฐ ์‹œ์ž‘)
date.getFullYear(); // ๋…„๋„๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
date.getDate(); // ์ผ์„ ๋ฐ›์•„์˜จ๋‹ค.
date.getHours(); // ์‹œ๊ฐ„์„ ๋ฐ›์•„์˜จ๋‹ค.
date.getMinutes(); // ๋ถ„์„ ๋ฐ›์•„์˜จ๋‹ค.
date.getSeconds(); // ์ดˆ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.

=> RESULT
Tue May 04 2021 11:08:35 GMT+0900 (๋Œ€ํ•œ๋ฏผ๊ตญ ํ‘œ์ค€์‹œ)
Thu Jun 24 1999 11:33:20 GMT+0900 (๋Œ€ํ•œ๋ฏผ๊ตญ ํ‘œ์ค€์‹œ)
Fri Oct 12 2018 12:11:22 GMT+0900 (๋Œ€ํ•œ๋ฏผ๊ตญ ํ‘œ์ค€์‹œ)

7. [Javascript] Interval

<div class="title">
  <h1>ํ˜„์žฌ์‹œ๊ฐ„์€</h1>
</div>
<div class="containers">
  <p id="current"></p>
</div>
<script type="text/javascript">
  function currentTime() {
    document.getElementById('current').innerHTML = new Date();
  }

  setInterval("currentTime()", 1000);
</script>
  • setInterval("currentTime()", 1000); ์€ currentTime() ํ•จ์ˆ˜๋ฅผ 1์ดˆ์— ํ•œ๋ฒˆ์”ฉ ์‹คํ–‰์‹œํ‚จ๋‹ค.
<form name="form1">
  ์ „ํ™”ํ•˜๋Š” ์‹œ๊ฐ„์€
  <input type="text" size="10" name="formSec"> ์ž…๋‹ˆ๋‹ค.

  <br>
  
  ์ „ํ™” ์š”๊ธˆ์€
  <input type="text" size="10" name="formWon"> ์ž…๋‹ˆ๋‹ค.
</form>

<script type="text/javascript">
  var myCount = 1;

  function func() {
    document.form1.formSec.value = myCount + "์ดˆ";
    document.form1.formWon.value = ((Math.floor(myCount/5) * 10) + 10) + "์›";
    myCount = myCount + 1;
  }

  setInterval("func()", 1000);
</script>
  • setInterval("func()", 1000); ์€ func() ํ•จ์ˆ˜๋ฅผ 1์ดˆ์— ํ•œ๋ฒˆ์”ฉ ์‹คํ–‰์‹œํ‚จ๋‹ค.

8. [javascript] ๋ฌธ์ž์—ด ํ•จ์ˆ˜

.indexOf() : ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
.lastIndexOf() : ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
.search() : ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
.slice() : ์•ž index๋ถ€ํ„ฐ ๋’ค index๊นŒ์ง€์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
.substring() : ์•ž index๋ถ€ํ„ฐ ๋’ค index๊นŒ์ง€์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
.charAt() : ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

9. [javascript] Event์ฒ˜๋ฆฌ

javascript๋Š” event๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. ํŠน์ • ํƒœ๊ทธ์— ํŠน์ • ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉํ•˜๊ณ  ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

onchange ( select and option ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•˜๋Š” ์ฝ”๋“œ )

<form action="" name="form1">
 <select name="links" onchange="goLink()">
	<option selected disabled hidden>Choose here !</option>
	<optgroup label="portal">
		<option value = "https://www.google.com">Google</option>
		<option value = "https://www.naver.com">Naver</option>
		<option value = "https://www.daum.net">Daum</option>
	</optgroup>
	
	<optgroup label="musci">
		<option value = "https://www.genie.co.kr">Genie</option>
		<option value = "https://www.melon.com">Melon</option>
		<option value = "https://www.mnet.com">Mnet</option>
	</optgroup>
 </select>
</form>

<script type="text/javascript">
function goLink() {
	var n = document.form1.links.selectedIndex;
	
	if (n != 0){
		location.href=document.form1.links.options[n].value;
	}
}
</script>

onload

  • ๋ฌธ์„œ์˜ ๋ชจ๋“  ์ฝ˜ํ…์ธ ๊ฐ€ ๋กœ๋“œ๋œ ํ›„ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ์ด๋‹ค.
window.onload \= function() { //์‹คํ–‰๋  ์ฝ”๋“œ
}
  • ์š”์†Œ์— ์†์„ฑ(attribute)์œผ๋กœ ์ง€์ •๋  ์ˆ˜ ์žˆ๋‹ค.
<body onload\="์‹คํ–‰๋  ์ฝ”๋“œ"\> 
  • ๊ฐ์ฒด(Object)๊ฐ€ ๋กœ๋“œ๋˜์—ˆ์„๋•Œ ์‹คํ–‰๋  ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
document.getElementById("myFrame").onload \= function() { //์‹คํ–‰๋  ์ฝ”๋“œ
}
  • jQuery์—์„œ๋Š” onload์™€ ๊ฐ™์€ ๋™์ž‘์„ ํ•˜๋Š” load ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
$(window).load( function() { //์‹คํ–‰๋  ์ฝ”๋“œ
})
//์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
<script>
  window.onload = function(){
  var a = document.getElementById( 'test' );
  a.style.color = "blue"
  }
</script>
<p id = "test">hello</p>
//์ œ์ด์ฟผ๋ฆฌ
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script>
$(document).ready(function(){
    var a = $( '#test' );
    a.style.color = "blue"
  });
}

== $(function(){
  var a = $( '#test' );
  a.style.color = "blue"
});
  • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์›น ๋ฌธ์„œ๋ฅผ ์ฝ๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด DOM์ด ์ƒ์„ฑ๋œ๋‹ค.
    DOM์ด๋ž€, Document Object Model - ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ์ด๋‹ค.
    ๋ฌธ์„œ๋Š” HTML๊ณผ XML๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ๋ฌธ์„œ๋ผ ํ•˜๊ณ  ์ด๋Ÿฌํ•œ ๋ฌธ์„œ๋ฅผ ๊ตฌ์กฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด DOM์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
    DOM(HTML์ด๋‚˜ XML๋ฌธ์„œ)์ด ์ƒ์„ฑ๋˜๋ฉด, $(document).ready(function( ~~~ ์ด ์‹คํ–‰๋œ๋‹ค.
    ๊ทธ ํ›„, ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋˜๋ฉด ํŽ˜์ด์ง€ ๋กœ๋”ฉ์€ ์™„๋ฃŒ๋˜๊ณ  $(window).load(function( ~~ ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

onblur

  • ํฌ์ปค์Šค๊ฐ€ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™์ด ๋˜์—ˆ์„๋•Œ ์‹คํ–‰๋œ๋‹ค.
<form action="#">
age: <input type="text" onblur="isRegNum(this)" size="3" maxlength="3"> years old. <br>
	write only number.

</form>
<script type="text/javascript">
/* object ๊ฐ€ ๋„˜์–ด์˜จ๋‹ค. */
function isRegNum(obj) {
	var str = obj.value;

	// ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •๊ทœ์‹
	if(str.match(/[^0-9]/g)){
		alert("๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.");
		obj.value="";
		return false;
	}
}
</script>
profile
แด…แด‡ฬแด แด‡สŸแดแด˜แด˜แด‡แดœส€. แด˜ส€แดษขส€แด€แดแดแด€แด›ษชแดษด ๐Ÿ”ฅ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€