[Day6] JS 6 [11/25]
์ปฌ๋ ์ ํด๋์ค (set, map)
set
1. set ๊ฐ์ฒด๋ฅผ ์์ฑ : new Set()
2. ํน์ง : ์ค๋ณตํ์ฉ X, ์์ ์ ์ง X
3. ์์ ์ถ๊ฐ : add();
3. ํฌ๊ธฐ : size
4. ๋ชจ๋ ์์ ์ฒ๋ฆฌ : values() ir(๋ฐ๋ณต์ ๋๋ ค์ค)
5. delete() - ์์ ์ ๊ฑฐ
6. has() - set ์์ ์์ ๋ฅผ ๊ฐ์ง๊ณ ์๋์ง t/f
7. forEach()
8. clear()
var s = new Set();
s.add("a");
s.add("b");
s.add("c");
var s = new Set(["aa", "bb", "cc"]);
s.forEach( function (el){
console.log( el );
} );
s.add("b"); // ์ค๋ณต ์ถ๊ฐ
s.add("b"); // ์ค๋ณต ์ถ๊ฐ
s.add("b"); // ์ค๋ณต ์ถ๊ฐ
console.log( s.size ); // 3
// b ์์๊ฐ ์๋์ง ํ์ธํด์ ์ญ์ (์ ๊ฑฐ)
if( s.has("b") ){
s.delete("b");
}
var ir = s.values();
for (let el of ir) {
console.log( el );
}
ex) ๋ก๋ ์ถ๋ ฅ
<button>๋ก๋ ๋ฒํธ ๋ฐ์๊ธฐ</button>
<p id="demo"></p>
// [js_5์ผ์ฐจ_๋ฌธ์ ์ 3-1 ์ฝ๋ฉ์ Set ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํด์ ์์ ]
$("button").click( function (){
var lotto = new Set() ; // var lotto = []; 1
var n ;
while ( lotto.size < 6 ) { // 2
n = getRndRange( 1, 45);
// 3. ์ค๋ณต ์ฒดํฌ X
lotto.add( n ); // 4 lotto.push( n ) X
} //
// array lotto.sort( (a,b)=> a-b); ์ ๋ ฌ
// Set, Map ์ ๋ ฌํ๊ณ ์ ํ๋ค๋ฉด -> ๋ฐฐ์ด ๋ณํ + ์ ๋ ฌ -> Set, Map ๋ณํ
var lottoArray = [];
lotto.forEach( function (el){
lottoArray.push( el );
} );
lottoArray.sort((a,b) => a-b );
$("#demo").html( "<ul><li>" + lottoArray.join("</li><li>") +" </li></ul>");
});
// 1. min ~ max ๋ฐ์์ํค๋ ํจ์
function getRndRange( min, max){
return Math.floor( Math.random()*(max-min+1) ) + min;
}
map
1. Map ํน์ง - key, value ํ ์(entry)์ผ๋ก ์ ์ฅ.
2. key ์ค๋ณต X
3. new Map() ๋งต ๊ฐ์ฒด ์์ฑ
4. size
5. set( key, value) : ์ํธ๋ฆฌ ์ถ๊ฐ
6. get( key ) : ์ํธ๋ฆฌ ์ป์ด์ค๊ธฐ
7. has(key)
8. delete( key )
9. forEach()
10. entries()
var m = new Map( [
["apples", 500],
["bananas", 300]
] );
console.log( m.size );
m.set("oranges", 700);
console.log( m.get("oranges") );
// ๋ชจ๋ entry ์ถ๋ ฅ.
m.forEach( function (value, key){
console.log( value + " / " + key );
});
// entries()
for (let entry of m.entries() ) {
// console.log( entry ); // key, value []
var key = entry[0];
var value = entry[1];
console.log( key + " / " + value );
}
js ์๋ฃํ
[ js ์๋ฃํ ( 5๊ฐ์ง ) ]
number
string
boolean
object
function
[js ๊ฐ์ฒด( 6๊ฐ์ง )]
Object
Date
Array
String
Number
Boolean
[๊ฐ์ ํฌํจํ ์ ์๋ 2๊ฐ์ง ์๋ฃํ]
null
undefined
[ js ํ ๋ณํ ]
1. ๋ฌธ์์ด -> ์ซ์ ํ๋ณํ
Number()
parseInt()
parseFloat()
eval()
2. ์ซ์ -> ๋ฌธ์์ด ํ๋ณํ
+ ""
String(123)
123.toString()
3. ๋ ์ง -> ์ซ์ ํ๋ณํ
var d = new Date();
Number( d ) ; // 1970.1.1 00:00:00.000 ~
d.getTime() ; // 1970.1.1 00:00:00.000 ~
4. ๋ ์ง - > ๋ฌธ์์ด ํ๋ณํ
String( Date() )
Date().toString()
5. ๋ถ๋ฆฐํ -> ์ซ์ ๋ณํ
Number(true) // 1
Number(false) // 0
6. ๋ถ๋ฆฐํ -> ๋ฌธ์์ด ๋ณํ
String(true) -> "true"
false.toString() -> "false"
7. ๊ธฐ์ต
5 + null => 5
"5" + nulll => "5null"
5 + "2" => "52"
5 * "2" => 10
8. js ๊ฐ์ฒด
๊ฐ์ฒด.toString()
* ์๊ฒฉ๋ชจ๋ ์ ์ธ
"use strict"; // ES5 ์๋ก ์ถ๊ฐ๋ ๊ธฐ๋ฅ
// ๋ณ์๋ฅผ ์ ์ธํ๊ณ ์ฌ์ฉํ๊ฒ ๋ค.
function xxx(){
"use strict"; // ํจ์ ๋ด์์ ์๊ฒฉ ์ ์ธ ๋ชจ๋
}
* ํธ์ด์คํ (hoisting)
์ ์ธ์ ๋งจ์๋ก ์ฎ๊ธฐ๋ js ๊ธฐ๋ณธ ๋์ : ๋ณ์, ํจ์
name = "admin";
mytest();
var name;
console.log( name );
function mytest(){
alert( "mytest...");
}
* ๋ฒ์(scope)
[ ๋ณ์์ ์ ๊ทผ ๋ฒ์ ]
1. global scope( ์ ์ญ๋ณ์ )
2. function scope( ํจ์๋ณ์== ์ง์ญ๋ณ์)
3. block scope( ์ง์ญ๋ณ์ ) : let, const
์์ธ์ฒ๋ฆฌ(์ค๋ฅ)
try
catch
finally
throw
try {
//
// test('hello world');
// 0<= kor <= 100
throw "too low";
//
} catch (e) {
// $("#demo").html( e.message );
$("#demo").html( e );
} finally{
}
console.log("end")
this ์๋ฏธ 4๊ฐ์ง
// 1. js ๊ฐ์ฒด ์์์ this => ๊ฐ์ฒด ์๊ธฐ ์์
var person = {
name:"admin",
info: function (){
return this.name;
}
}
// 2. ์ด๋ฒคํธ ํธ๋ค๋ฌ ์์์ this => ์ด๋ฒคํธ๋ฅผ ๋ฐ์ ๊ฐ์ฒด
document.querySelector("h3").onclick = function (){
alert( this );
}
// 3. ํจ์ ์์์ this => [object Window]
function test(){
return this;
}
// 4. [object Window]
var x = this;
ํ์ดํ ํจ์( ๋๋ค์ )
// => ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ํจ์๋ฅผ ๋ ์งง์ ๊ตฌ๋ฌธ์ผ๋ก ์์ฑํ ์ ์๋ค.
// ES6 ๋์
var hello = function (){
// 1
// 2
// 3
return "hello world";
};
// ๋๋ค์
// { } ์์ return ๋ฌธ๋ง ์๋ ๊ฒฝ์ฐ์๋ {} ์ค๊ดํธ ์๋ต + return ๋ฌธ ์๋ต
// var hello = () => "hello world" ;
var hello = function ( name ){
return "hello world " + name;
};
// ๋งค๊ฐ๋ณ์๊ฐ 1๊ฐ์ธ ๊ฒฝ์ฐ์๋ง () ๊ดํธ๋ฅผ ์๋ตํ ์ ์๋ค.
// var hello = ( name ) => "hello world " + name;
// var hello = name => "hello world " + name;
var hello = () => {
// ๋๋ค์์์์ this๋ ? ํจ์์ ์์ ํ์..
console.log( this );
}
console.log( hello() );
ํด๋์ค
1. js์๋ class ํค์๋ ์ฌ์ฉ. ( ES6 )
2. ๊ฐ์ฒด ์ค๊ณ๋( ํ ํ๋ฆฟ )
class Car {
// ํ๋
// private String name;
// private int age;
constructor(name, year) { // js ์์ฑ์ : ๊ฐ์ฒด ์์ฑํ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์
this.name = name;
this.year = year;
}
// ๋ฉ์๋
age( year ){
return year - this.year;
}
}
var myCar1 = new Car("Volvo", 2019);
var myCar2 = new Car("Audi", 2020);
console.log( myCar1.name + " / " + myCar1.age(2022) );
js ๋ชจ๋
- js ๋ชจ๋์ ์ฌ์ฉํ๋ฉด ์ฝ๋๋ฅผ ๋ณ๋์ ํ์ผ๋ก ๋๋์ด ์ ์ฅ -> ๊ด๋ฆฌ(์ ์ง, ๋ณด์)
๋ณ์ , ํจ์
- import(์์ ) / export(์์ถํ๋ค) ๋ฌธ์ ์ฌ์ฉ
- ex08_person.js : ๋ณ์
- ex08_message.js : ํจ์
/**
* ex08_message.js
*/
const message = () => {
const name = "hong";
const age = 30;
return name + " is " + age + "years old.";
}
export default message;
/**
* ex08_person.js
* ๋ณ์ ๋ด๋ณด๋ด๊ธฐ(export)
*/
/*
export const name = "admin";
export const age = 20;
*/
const name = "admin";
const age = 20;
export { name, age }
JSON
JSON ๋ฌธ๋ฒ ์ฌ์ดํธ: https://www.json.org/json-en.html
์ฌ์ฉ: ***** JSP + [jquery ajax] + [JSON] ์ฌ์ฉ *****
- JSON ( JavaScript Object Notation )
- ๊ฐ๋ฒผ์ด [ ๋ฐ์ดํฐ ๊ตํ ํ์ ] ์๋ ์ XML ๋ง์ด์
- ์ธ๊ฐ์ด ์ฝ๊ณ ์ฐ๋ ๊ฒ์ ์ฝ๋ค. ๊ธฐ๊ณ๊ฐ ์ฝ๊ฒ ๊ตฌ๋ฌธ ๋ถ์ํ๊ณ ์์ฑํ ์ ์์ต๋๋ค
- ๋ชจ๋ ์ธ์ด์์ JSON ์ฌ์ฉ. ( java, js, c#, python ๋ฑ๋ฑ)
js ๊ฐ์ฒด(๊ฐ์ฒด) ***
1. js ๊ฐ์ฒด๋ ๊ธฐ๋ณธํ์ ์ ์ธํ๋ฉด ๋ชจ๋ js ๊ฐ์ฒด์ด๋ค.
2. js ๊ธฐ๋ณธํ ? string, number, boolean, null, undefined, bigint,
3. js ๊ฐ์ฒด ? new String(), new Number(), new Boolean()
new Date,
Math
Array
function
object
๋ฑ๋ฑ
4. js ๋ฐฐ์ด = []
js ๊ฐ์ฒด = {}
js ๊ฐ์ฒด๋ ๋ณ์์ด๋ค.
var person = {
// ์์ฑ
name:"admin",
age:20,
// ๊ฐ์ฒด์ ๋ฉ์๋
info:function (){}
}
5. js ๊ฐ์ฒด ์์ฑ ๋ฐฉ๋ฒ ( 4๊ฐ์ง )
1) new ํค์๋
new Date()
new Car()
var peron = new Object(); // ๋น ๊ฐ์ฒด ์์ฑ
person.name = "admin"; // ์์ฑ ์ถ๊ฐ
person.age = 20;
2) ๊ฐ์ฒด ๋ฆฌํฐ๋ด
var peron = { name:"admin", age:20};
var peron = {}; // ๋น ๊ฐ์ฒด ์์ฑ
person.name = "admin"; // ์์ฑ ์ถ๊ฐ
preson.age = 20;
3) ๊ฐ์ฒด ์์ฑ์
4) Object.create()
6. js ๊ฐ์ฒด ์์ฑ
1) ์ ๊ทผ
๊ฐ์ฒด๋ช .์์ฑ๋ช ;
person.name
๊ฐ์ฒด๋ช
["์์ฑ๋ช
"];
person["name"];
2)for in ๋ฌธ
for( let p in object) {
์์ฑ๋ช
}
ex)
var peron = {
firstname:'john',
lastname:'Doe',
age:40,
eyecolor:'blue',
fullName:function (){
return this.firstname +" " + this.lastname;
}
}
// 1. ์๋ก์ด ์์ฑ(ํ๋) ์ถ๊ฐ.
// ๊ฐ์ฒด๋ช
.์๋ก์ด์์ฑ๋ช
=์์ฑ๊ฐ;
person.nationality = "kerea";
// 2. ์์ฑ ์ ๊ฑฐ
// delete ๊ฐ์ฒด๋ช
.์์ฑ๋ช
;
delete person.eyecolor;
// delete person["eyecolor"];
// 3. ๋ฉ์๋ ํธ์ถ
// ๊ฐ์ฒด๋ช
.๋ฉ์๋๋ช
();
var name = person.fullName();
console.log( name );
// 4. ์๋ก์ด ๋ฉ์๋ ์ถ๊ฐ.
// ๊ฐ์ฒด๋ช
.๋ฉ์๋๋ช
= function (){}
person.name = function (){
return this.firstname +" / " + this.lastname;
}
// 5. ๋ชจ๋ ์์ฑ ์ถ๋ ฅ
for ( var p in person) {
console.log( p + " / " + person[p]); // person.p X typeof string
}
//
/*
var person = {}
var person = new Object(); ๊ถ์ฅ X
person.firstname = "john";
person.fullName = function(){
}
*/
//var name = "admin";
//var name = new String( "admin"); X
[ ์ค์ฒฉ๋ ๊ฐ์ฒด ]
var person = {
name:"admin",
age:20,
cars: [
{ name:"BMW", models:["a","b","c"]},
{ name:"KIA", models:["a","b","c"]},
{ name:"Ford", models:["a","b","c"]}
]
};
// person.name
// person.cars[0].name BMW
var person = {
name:"admin",
age:20,
cars: {
car1 : "BMW",
car2: "KIA",
car3: "FORD"
}
};
// KIA
person.cars.car2;
person.cars["car2"];
person["cars"]["car2"];
const myObj = {
name: "John",
age: 30,
cars: [
{name:"Ford", models:["Fiesta", "Focus", "Mustang"]},
{name:"BMW", models:["320", "X3", "X5"]},
{name:"Fiat", models:["500", "Panda"]}
]
}
for (let i in myObj.cars) {
x += "<h1>" + myObj.cars[i].name + "</h1>";
for (let j in myObj.cars[i].models) {
x += myObj.cars[i].models[j];
}
console.log( x );
}
[ js ๊ฐ์ฒด๋ฅผ ํ์(์ถ๋ ฅ)ํ๋ ๋ฐฉ๋ฒ ]
1. ๊ฐ์ฒด๋ช .์์ฑ๋ช person.name
2. ๋ฐ๋ณต๋ฌธ ์ฌ์ฉ - ์์ฑ๋ช /์์ฑ๊ฐ for( let p in person){}
3. ์์ฑ์ ๊ฐ(value)๋ค์ ๋ฐฐ์ด๋ก ๋ฐํํ๋ ํจ์ : Object.values() ์ฌ์ฉ.
4. js ๊ฐ์ฒด -> JSON (๋ฌธ์์ด)๋ณํ : JSON.stringify()
[] stringify : ๋์ผ๋ก ๋ฌถ๋ค.(์๋ค)
var person = { // js object
name:"admin",
age:20,
city:"seoul",
today:new Date(), // ๋ ์ง ๊ฐ์ฒด
cars:["bmw", "kia", "volvo"], // ๋ฐฐ์ด -> ๋ฌธ์์ดํ ๋์ด์ง๋ค.
// method
print: function (){
return "print()...";
}
};
/*
// console.log( person ); // [object ]
var outArr = Object.values( person );
// alert( typeof out ); ๋ฐฐ์ด ์ฒดํฌ
alert( outArr ); // admin,20,seoul
*/
// [ JSON ํ๊ธฐ๋ฒ์ ๋ฐ๋ฅด๋ ๋ฌธ์์ด ]
person.print = person.print.toString();
var jsonStr = JSON.stringify( person ); // jsp/servlet + jquery ajax ์ฒ๋ฆฌ
// {"name":"admin","age":20,"city":"seoul"}
// { "์์ฑ๋ช
":"์์ฑ๊ฐ", "์์ฑ๋ช
": }
// ๋ ์ง ๋ฌธ์์ดํ
// {"name":"admin","age":20,"city":"seoul","today":"2022-11-25T03:13:10.882Z"}
// print ๋ฉ์๋ ์ถ๊ฐ -> JSON ๋ณํ ๋ฉ์๋๋ ๋ฌธ์์ดํ ๋์ง ์๋๋ผ..
alert( jsonStr );
// js ES6 getter, setter ๋์ .
// java private member -> getter, setter ์ ์ธ.
var person = {
firstname:"john",
lastname:"Doe",
language:"kr",
fullName:function (){
return this.firstname + " " + this.lastname;
},
// getter
get name () {
return this.firstname + " " + this.lastname;
},
// getter, setter
get lang() {
return this.language.toUpperCase();
},
set lang( lang ) {
this.language = lang.toUpperCase();
}
}
// alert( person.name)
person.lang = "en"; // setter
// ํจ์์ธ๋ฐ ์ ํธ์ถํ ๋ () ๋ฅผ ์๋ถ์ฌ๋ ๋๋๊ฑด๊ฐ์? ๊ฒํฐ ์ธํฐ๊ฐ ํน์ํ ๊ฒฝ์ฐ์ธ๊ฑด๊ฐ์?
alert( person.lang ); // getter
// alert( person.fullName() )
[ js ๊ฐ์ฒด ์์ฑ์ ]
- ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์ฌ์ฉ๋๋ ํจ์๋ฅผ "๊ฐ์ฒด ์์ฑ์"๋ผ๊ณ ํ๋ค.
- ๋๋ฌธ์๋ก ์ด๋ฆ์ ์์ํ๋ค. ( ๊ถ์ฅ )
// js ํจ์
// this ๋ ์์ฑ๋ ๋์ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ํ๋ธ๋ค.
// "๊ฐ์ฒด ์์ฑ์ ํจ์"
function Person( fname, lname, age ){
this.firstname = fname;
this.lastname = lname;
this.age = age;
// ๋ฉ์๋
this.name = function (){
return this.firstname +" " + this.lastname;
}
}
var myFather = new Person("john", "Doe", 40);
var myMother = new Person("Sally", "Doe", 36);
// 1. ๊ฐ์ฒด์์ฑ์ํจ์ -> ์์ฑ, ๋ฉ์๋ ์ถ๊ฐ
// Person.eyecolor ="blue"; X
//Person.prototype.eyecolor= "blue";
// 2. ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ํตํด์ -> ์์ฑ, ๋ฉ์๋ ์ถ๊ฐ
//myFather.eyecolor = "blue";
// 3. ๊ฐ์ฒด์์ฑ์ํจ์ -> ์์ฑ, [๋ฉ์๋] ์ถ๊ฐ
// Person.prototype.print = function (){}
myFather.print = function (){}
js ํจ์
1. ํจ์ ์ ์
2. ํจ์ ์ ์ธ ํ์
function ํจ์๋ช
( [๋งค๊ฐ๋ณ์...]){
[return ๋ฆฌํด๊ฐ;]
}
3. Function() ์์ฑ์
function sum(a,b){ return a+b; }
var sum = new Function("a", "b", "return a+b");
console.log( sum(1,2) );
*** ์ต๋ช ์ ์์ฒด ํธ์ถ ํจ์ ***
1) ํจ์ ์ ์ธ + 2) ํจ์ ํธ์ถ
// 1) ํจ์ ์ ์ธ
function print(){
console.log("print()...");
}
// 2) ํจ์ ํธ์ถ
print(); // ํธ์ถ
// 1) ํจ์ ์ ์ธ
( function (){
console.log( "test ");
} )(); // 2) ํจ์ ํธ์ถ
// ์๋ฃํ X ๋งค๊ฐ๋ณ์
// ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ์ ์๋ค.
function disp( name = "hong", age = 20){
// arguements ์ปฌ๋ ์
console.log(" xxx ");
}
disp(); // undefined , undefined
disp("admin");
disp("admin", 20);
disp("admin", 20, true);
java : public static int sum( int...args){}
function sum(...args){
let result = 0;
for (let n of args) {
result += n;
}
return result;
}
console.log( sum(1,2,3) );
console.log( sum(1,2,3,4,5,6) );
var m = [3,5,2,4,1];
// console.log( sum( m ) );
call(), apply()
๋ค๋ฅธ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ call() ๋ฉ์๋๋ก ๋ถ๋ฌ์ ์ฒ๋ฆฌํ ์ ์๋ค.
๋ค๋ฅธ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ apply() ๋ฉ์๋๋ก ๋ถ๋ฌ์ ์ฒ๋ฆฌํ ์ ์๋ค.
person printf person1, person2
[์ฐจ์ด์ ] ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๊ฒฝ์ฐ
call ๋์ด
apply ๋ฐฐ์ด
var person1 = {
name:"hong",
age:20
/*
printf:function (){
return this.name + "/" + this.age;
}
*/
};
var person2 = {
name:"admin",
age:45
/*
printf:function (){
return this.name + "/" + this.age;
}
*/
};
var person = {
// ์์ฑ
// this.eyecolor = "blue"
// ๋ฉ์๋
printf:function ( city , eyecolor ){
return this.name + "//" + this.age + "//" + city + "//" + eyecolor;
},
println:function ( ){
return this.name + "//" + this.age ;
}
}
//console.log( person1.printf() );
//console.log( person2.printf() );
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ call() ๋ฉ์๋ ์ฌ์ฉ๋ฒ.
console.log( person.println.call( person1) );
console.log( person.println.apply( person2) );
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ call() ๋ฉ์๋ ์ฌ์ฉ๋ฒ.
console.log( person.printf.call( person1, "seoul" , "blue") );
// console.log( person.printf.apply( person2, "pohang" , "green") );
console.log( person.printf.apply( person2, [ "pohang" , "green" ]) );
// console.log( (person.println.bind( person2))() ); // OK
//
// Math.max.apply( 0 "" Math , [1,5,3,2] );
// Math.max.call( null, 1,5,3,2 );
bind()
- bind : ๋ฌถ๋ค. ๊ฐ์ธ๋ค.
- ๋ค๋ฅธ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ๋น๋ฆฐ๋ค. + ์คํ X
- call()/apply()๊ณผ bind() ์ฐจ์ด์ .
var person = {
name:"admin",
age:20,
print:function (){
return this.name + " / " + this.age;
}
}
// member ๊ฐ์ฒด์๋ print() ๋ฉ์๋๊ฐ ์๋ค
var member = {
name:"kim",
age:34
}
//console.log( person.print.call( member ) ); // kim / 34
//console.log( person.print.apply( member ) ); // kim / 34
var fnprint = person.print.bind(member);
console.log( fnprint() ); // kim / 34
/*
ƒ (){
return this.name + " / " + this.age;
}
ํจ์ ์ฐจ์ฉ + ํธ์ถ() X
*/
/*
function sum(){
console.log("ํฉ");
}
console.log( sum )
ƒ sum(){
console.log("ํฉ");
}
*/
setTimeout()/clearTimeout()/[setInterval()/clearInterval()]
ex1) ์ฌ๊ท
<h1 id="demo"></h1>
<button>setTimeout</button>
<button>clearTimeout</button>
$("button").first().on( "click" , function (){
//alert("1")
dispTime();
});
var timer1;
function dispTime(){
var now = new Date();
$("#demo").html( now.toLocaleString() );
// setTimeout( ํจ์ ํธ์ถ X , ํจ์๋ฑ๋ก O , ms)
// 1000ms(1์ด) ํ์ ๋ฑ๋ก๋ ํจ์๋ฅผ 1๋ฒ ํธ์ถํ๋ ๋ฉ์๋
timer1 = setTimeout( dispTime , 1000);
}
$("button").last().on( "click" , function (){
//alert("2")
clearTimeout(timer1);
});
ex2) ์ฌ๊ทX
<h1 id="demo"></h1>
<button>setInterval</button>
<button>clearInterval</button>
$("button").first().on( "click" , function (){
dispTime();
// 1์ด ๋ง๋ค ๋งค๋ฒ ๋ฑ๋ก๋ํจ์( dispTime )๋ฅผ ํธ์ถํ์ธ์.
timer1 = setInterval( dispTime , 1000);
});
var timer1;
function dispTime(){
var now = new Date();
$("#demo").html( now.toLocaleString() );
}
$("button").last().on( "click" , function (){
clearInterval(timer1);
});
ex3) html ์๋์ผ๋ก ์ฝ์ด์ค๊ธฐ
<button>TypeWriter</button>
<p id="demo"></p>
var content ;
var contentLength ;
var timer1;
$("button").on("click" , function (){
// document.documentElement == <html> ๋ฃจํธ์์(element)
//content = document.documentElement.innerHTML;
content = $("html").html();
contentLength = content.length; // ๋ฌธ์์ด ๊ธธ์ด
// alert( content )
typeWriter();
timer1 = setInterval( typeWriter , 10);
});
var i = 0;
function typeWriter(){
if( i <= contentLength ){
var oneChar = content.charAt( i++ );
$("#demo").html( function (index, oldhtml){
return oldhtml + ( oneChar =='\n' ? "<br>" :oneChar ) ;
});
// window.scrollTo(0, 0); ๋งจ์๋ก ์คํฌ๋กค ์ฌ๋ฆฌ๊ธฐ
window.scrollTo(0, document.body.scrollHeight );
}else{
clearInterval(timer1);
alert("์ถ๋ ฅ ์๋ฃ!!!");
}
}
์คํฌ๋กค ํ์ํ๊ธฐ
-css-
<style>
body{
margin: 0;
font-size: 28px;
}
.header{
position: fixed;
top:0;
z-index: 1;
width:100%;
background-color: #f1f1f1;
}
.header h2 { text-align: center;}
.process-container{
width:100%;
height:8px;
background:#ccc;
}
.progress-bar{
height:8px;
background-color: red;
width:0;
}
.content{
padding:100px 0;
margin:50px auto 0 auto;
width:80%;
}
</style>
<style>
#btnTop{
position: fixed;
bottom:20px;
right:30px;
z-index: 99;
outline: none;
background: red;
color:white;
padding:15px;
border-radius: 10px;
display: none;
border:none; /* ๋ฒํผ์ ๊ธฐ๋ณธ์ ์ผ๋ก ํ
๋๋ฆฌ๊ฐ ์๋ค.. */
}
#btnTop:hover{
background: #555;
}
</style>
-html-
<div class="header">
<h2>์คํฌ๋กค ํ์ํ๊ธฐ</h2>
<div class="process-container">
<div class="progress-bar" id="myBar"></div>
</div>
</div>
<!-- .content>p*40>lorem8 -->
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p>Repellat praesentium ex animi eos in excepturi quod?</p>
<p>Quas a commodi labore quidem nihil ipsum reiciendis.</p>
<p>Eum quisquam laboriosam modi impedit ipsam iste consectetur.</p>
<p>Minima accusantium architecto minus ex odit exercitationem similique.</p>
<p>Modi dolores quo ipsa voluptate eius ratione eum!</p>
<p>Rem vel tempora repudiandae quod consequatur fugit sequi.</p>
<p>Expedita assumenda eius molestias eum quas nisi aliquam!</p>
<p>Tempora deleniti magnam minus ullam officiis tempore dolor!</p>
<p>Molestias obcaecati eum impedit facere vel. Dolorem quod.</p>
<p>Autem mollitia dolores vitae aspernatur consequatur ratione quis?</p>
<p>Neque veritatis ut iste sequi debitis ex iure?</p>
<p>Aliquam corporis dolore voluptatibus quas dolorum maxime animi.</p>
<p>Officiis odit temporibus non assumenda laudantium quam labore.</p>
<p>Id eum explicabo nulla quidem necessitatibus eos sapiente.</p>
<p>Est quibusdam aliquid doloribus voluptatum iusto quis doloremque.</p>
<p>Doloremque rem non voluptas quaerat rerum veniam odit?</p>
<p>Dicta libero ab est incidunt accusantium voluptatum praesentium.</p>
<p>Cumque est culpa provident distinctio consectetur voluptatem nobis.</p>
<p>Explicabo ex error similique est magni nihil deleniti!</p>
<p>Doloribus repellendus quos deleniti debitis delectus reprehenderit fuga!</p>
<p>Consequatur quia architecto impedit aspernatur tempore alias dolores.</p>
<p>Ea temporibus cumque laudantium autem similique distinctio omnis.</p>
<p>Harum eligendi iusto saepe cumque tenetur delectus praesentium.</p>
<p>Laborum culpa iure aspernatur omnis ipsum unde deserunt?</p>
<p>Debitis voluptatibus distinctio maiores accusamus nesciunt facere voluptate!</p>
<p>Eum dignissimos ut delectus ab atque tempore quaerat.</p>
<p>Dicta ipsum esse quam modi eligendi possimus excepturi.</p>
<p>Nobis velit culpa reprehenderit sit ut unde voluptatibus.</p>
<p>Hic nostrum natus dolore laudantium odio aut et?</p>
<p>Officiis autem voluptatem eveniet neque veniam accusantium corporis.</p>
<p>Dolore eius id magni sint a quos ipsum.</p>
<p>Dignissimos blanditiis aliquid inventore tempora ratione dolore ab.</p>
<p>Officia aut ea cupiditate cumque non quae blanditiis.</p>
<p>Officiis nemo sit eos dicta necessitatibus incidunt aliquid.</p>
<p>Qui et libero nesciunt quia dolor quae optio.</p>
<p>Ipsam itaque reprehenderit blanditiis quam dignissimos magni delectus!</p>
<p>Amet officia dolorum temporibus maiores quos voluptates ea.</p>
<p>Voluptas omnis soluta culpa fuga cupiditate sunt iure.</p>
<p>Aut voluptas illo assumenda sed provident quod cupiditate!</p>
</div>
<!-- button#btnTop[title="Go To Top"]>{Top} -->
<button id="btnTop" title="Go To Top">Top</button>
-js-
// js ์ฝ๋ฉ.
window.onscroll = function (){
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop ; //
var scrollHeight = document.documentElement.scrollHeight;
var clientHeight = document.documentElement.clientHeight;
// console.log( scrollHeight +" / " + scrollTop + " / " + clientHeight);
var hiddenHeight = scrollHeight - clientHeight;
// ์จ๊ฒจ์ง ๋์ด๋ฅผ 100%๋ก ํ์ ๋ ์คํฌ๋กค์ฌ๋ฆด (scrollTop)๊ฒฝ์ฐ ์ด ๋ช % ํด๋นํ๋์ง ?
// 100:x = hH:sT
// x = ( 100*sT)/hH
var widthPercent = ( 100 * scrollTop ) / hiddenHeight;
document.getElementById("myBar").style.width = widthPercent +"%";
if ( scrollTop >= clientHeight ) {
document.getElementById("btnTop").style.display = "block";
} else {
document.getElementById("btnTop").style.display = "none";
}
} ;
// jquery ์์ ..
// scrollHeight
// jquery method : scrollTop()
//jquery method : innerHeight() ๋์ด + ํจ๋ฉ,๋ณด๋ O + ๋ง์ง X
//jquery method : Height() ๋์ด + ํจ๋ฉ,๋ณด๋ X
ih = innerheight = clientheight
offset์ด ๋ถ์ผ๋ฉด border, padding ํฉ์ณ์ง ๊ฒ
bind() ๋ค๋ฅธ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ๋ฌถ๋ค
<p id="demo"></p>
var person = {
firstname : "John",
lastname:"Doe",
display:function (){
let demo = document.getElementById("demo");
demo.innerHTML = this.firstname + " / " + this.lastname;
}
}
// person.display();
// 5์ด ๋ค์ display() ํจ์ ํธ์ถ(์คํ)
// undefined / undefined
// [๋ฌธ์ ์ ]
// [window].setTimeout( );
// setTimeout( person.display , 5000);
// bind() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ person.display ๋ฅผ person ์ ๋ฐ์ด๋ฉ...
var display = person.display.bind(person);
setTimeout( display , 5000);
* bind๋ก ๋ฌถ์ง ์์ผ๋ฉด undefined -> ๊ฐ์ฒด ์ this๊ฐ window ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด!
๊ทธ๋์ bind๋ก ๋ฌถ์ด์ค์ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๊ฐ person์ผ๋ก ํด์ค์ผํจ
[์ด๋ฒคํธ ๋ฒ๋ธ๋ง], ์ด๋ฒคํธ ์บก์ฒ
1. ์ด๋ฒคํธ ๋ฒ๋ธ๋ง
์์ ์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋ถ๋ชจ ์์์ ๊ทธ ์ด๋ฒคํธ ์ ๋ฌ(์ ํ). ๋ฐ์.
2. ์ด๋ฒคํธ ์บก์ฒ๋ง X
ex) p ํ๊ทธ ํด๋ฆญํ๋๋ฐ div ํด๋ฆญ๊น์ง ๋จน์
<!-- #out>h2+p#in -->
<div id="out">
<h2>์ด๋ฒคํธ ๋ฒ๋ธ๋ง ์ฒดํฌ</h2>
<p id="in">์์ p ํ๊ทธ</p>
</div>
$("#out").click( function (){
alert("out div click.");
});
$("#in").click( function (){
alert("in p click.");
// ์ด๋ฒคํธ ๋ฒ๋ธ๋ง ๋ง์๋ผ..
event.cancelBubble = true;
});
// table > tr > td
// js DOM , BOM -> jquery -> [JSP/Servlet] 2~3 ์ฃผ + [3์ฃผ] + ์คํ๋ง +AWS
์ต๊ทผ๋๊ธ