// 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์ด๋ค.
String map = superman;
map.substring(5,7);
man์ ๋ฆฌํดํ๋ค.
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);
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>
<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>
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
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 (๋ํ๋ฏผ๊ตญ ํ์ค์)
<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์ด์ ํ๋ฒ์ฉ ์คํ์ํจ๋ค..indexOf() : ํด๋น ๋ฌธ์์ด์ด ์๋ ์์น๋ฅผ ๋ฐํํฉ๋๋ค.
.lastIndexOf() : ํด๋น ๋ฌธ์์ด์ด ์๋ ์์น๋ฅผ ๋ค์์ ๋ถํฐ ์ฐพ์ ๋ฐํํฉ๋๋ค.
.search() : ํด๋น ๋ฌธ์์ด์ด ์๋ ์์น๋ฅผ ๋ฐํํฉ๋๋ค.
.slice() : ์ index๋ถํฐ ๋ค index๊น์ง์ ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค.
.substring() : ์ index๋ถํฐ ๋ค index๊น์ง์ ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค.
.charAt() : ํด๋น ์ธ๋ฑ์ค์ ๋ฌธ์๋ฅผ ๋ฐํํฉ๋๋ค.
javascript๋ event๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ค. ํน์ ํ๊ทธ์ ํน์ ์ด๋ฒคํธ๋ฅผ ์ ์ฉํ๊ณ ํ์ฉํ ์ ์๋ค.
<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>
window.onload \= function() { //์คํ๋ ์ฝ๋
}
<body onload\="์คํ๋ ์ฝ๋"\>
document.getElementById("myFrame").onload \= function() { //์คํ๋ ์ฝ๋
}
$(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"
});
<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>