[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. ์ด๋ฒคํŠธ ์บก์ฒ˜๋ง  

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

 

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ