소스 코드를 기록하는 남자

'variable'에 해당되는 글 3건

  1. JavaScript - Function Executing Process
  2. JavaScript - Global Variable
  3. JavaScript - Hoisting

JavaScript - Function Executing Process

JavaScript

Because of JavaScript's Declaration Process, Every Declarations will go to the Top of Code

without value

 

Without Value is very important. Let's look the example

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>함수의 동작원리 :: 선언적 함수 : 리턴타입+매개변수</title>
<script type="text/javascript">

	var calc = function (x, y) {//function declaration
		return x+y;
	}	
	console.log(calc(5,3)); //8
	
	

	var calc = function (x, y) {//function declaration
		return x*y;
	}
	console.log(calc(5,3)); //15
	
</script>
</head>
<body>

</body>
</html>

first console.log will print 8, second will print 15.

 

this is the process of this code.

 

1. var calc will go to the top

 

2. x + y function will be a value of calc.

 

3. first console.log print 5+3

 

4. x * y function will be a value of calc.

 

5. second console.log print 5 * 3

 

 

But it is different between function and variable, lets see the example

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>선언적 함수 정의 및 재정의 | 익명함수 정의 및 재정의</title>
<script type="text/javascript">

 	
	var func = function(){alert("func inner");}// 2 second create
	function func(){alert("func declear");} //1 first create
	func(); //func inner; 
	
	
</script>
</head>
<body>

</body>
</html>

for this code, variable func and function func() will go to the top.

 

so this process is

 

1. func will be go to the top with first create function

 

2. func is overrided with second create.

 

3. func() executing

'JavaScript' 카테고리의 다른 글

JavaScript - bool data type converting  (0) 2019.09.14
JavaScript - typeof(), prompt(), String(), Number()  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14

JavaScript - Global Variable

JavaScript

In javaScript, it is different from other langauge.

 

Lets see the code.

 

after showAge function called, the variable "age" is changed to a Global Variable 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">

	// Global Variable Declaration (3) a Global variable connected to window object
/* 	var myName = "James";
	//
	firstName = "Gosling";
	//
	var name;
	name = "James Gosling";
	
	console.log(myName +' '+firstName+" "+name); */
	
	//2. A Global Variable with function
	
	function showAge() {
		age = 90;
		//console.log(age);
	}
	showAge(); //after function called, age will be a global variable
	console.log(age);
	
	
</script>
</head>
<body>

</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - typeof(), prompt(), String(), Number()  (0) 2019.09.14
JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14

JavaScript - Hoisting

JavaScript

In JavaScript, there is a rule for Declaration.

 

Every variable will be hoisted to top. 

 

Remember! Just Declaration, Not a value

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>호이스팅 :: Hoisting</title>
<script type="text/javascript">
//1. hoisting
 	doSomething();
	
	function doSomething() { 
		alert("not defined : "+some); //undefined
	
		var some = "WellBeing";
		
		alert("defined : "+some); //WellBeing
	}; 
//2. Global Variable
var name = "James";
function showName(){
	var name = "Gosling";
	console.log(name);
}
console.log(name);
showName();
</script>
</head>
<body>

</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14
JavaScript - Datatype  (0) 2019.09.14