JavaScript/개인 스터디

[JavaScript] 기본 내용 정리

깍돌이 2018. 3. 19. 13:09

 > 자바스크립트는 함수형 언어이다. 

-> forEach(item,index,array)

 > 자바스크립트의 함수는 1종 객체이다. 


var show = function(){} 와 같이 함수를 변수로 생성 할수 있다.


show.add = functioin(){} 와 같이 마찬가지로 함수를 프로퍼티에 할당할수 있다.


위의 두개를 보고 알수 있듯이 프로퍼티와 메서드를 가질수 있다.


> 함수내의 선언된 타입의 유효범위는 함수 내 전체이다.


변수 참조


show; // ->undefined

var show = 1  // undefined로 올라와짐




show(); // -> undefined

var show = function(){}; // undefined로 


는 show가 함수지만 변수로 생성하였기 때문에 변수의 호출 방식을 따르며


undefined로 올라(호이스팅)온다.


show(); // show

function show() { console.log("show");} // show 함수 전체가 참조 될수있도록 올라간다.(호이스팅)



할당되지 않은 경우의 매개변수는 undefined



function(param,1param2) 로 선언후 



function(1,2,3,4,5) 로 진행시


param1 = 1

param2 = 2


3,4,5는 따로 연결되지 않는다.


function show(){} 후


> show() 시에 내부적으로 2개의 매개변수가 있다.


arguments:  실제로 전달된 인자의 컬렉션


this : 함수 콘텍스트 객체를 참조


function(param1,param2)

function(1,2,3,4,5)


시에 arguments를 사용하면 3,4,5까지 가져다 사용할수 있다.



			
	function hoisting(){

		console.log("a is :",a);
		console.log("b is :",b);
		
		typeof b ==='function' ? console.log("b Call ",b()) : console.error("b is :",b);
		console.log("c is : ",c);
		typeof c ==='function' ? console.log("c Call ",c()) : console.error("c is :",c);
		console.log("c Call ",c());
		
	var a =1;
	var b = function(){console.log("Function b");};
	function c (){
		console.log("Function C");
	}
	}
	hoisting();	


결과 




> apply 나 call 을 사용시 함수 콘텍스트를 변경할수 있다.