Javascript study notes (functions, arrays, objects)

function

Javascript function in the role of other languages, like, do not introduced, the definition of direct use.

//函数的定义格式,其中function是关键字,其余定义方式和python/Java 大同小异,其函数体使用{}包裹起来

function funt1(){
	//函数体
}

//函数调用
funt1();

variable

Javascript and most languages ​​are basically the same, divided into global and local variables.

  • Global variables: public, variables can be used throughout the Javascript code page, which is when the page is closed, go away
  • Local variables: definitions inside a function, use only inside the function, disappeared after the completion of function execution

By value

Javascript is divided into two by value, a copy of value and reference pass-pass (it means the depth of copies in python)

  • Copy by value: the value of the variable copy, then the original variables between the variables and replication is independent, nothing, modify one another does not change (common character type, value type variable by value)
  • Quote pass-: variable is assigned an address in memory to the new variables, but in fact the two variables pointing to the same address data, that is, the same data, any of which is modified, then the data is modified ( the common references have pass-arrays, objects ...)
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>js-function-2</title>
		<script type="text/javascript">
			// 全局变量与局部变量
			// 在函数外定义的变量都是全局变量,在函数内部定义的变量都是局部变量,只能在当前函数内部使用
			
			// 全局变量
			var stuNumber = '001';
			function stuFunct(){
				// 局部变量
				var stuName = '韩信';
				
				// 函数操作
				document.write('姓名'+stuName+'学号'+stuNumber);
			}
			
			// 函数调用
			stuFunct()
			
			// 错误方式
			window.alert(stuName);
		</script>
	</head>
	<body>
	</body>
</html>

Anonymous function

Anonymous function generally used for the function return value is assigned to the other variables, or binding a certain operation using a direct method using a write function () {}; no function name

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>匿名函数</title>
		<script type="text/javascript">
			// 定义方式
			
			// 1 给变量赋值
			var a = function(){
				
			}
			// 调用
			var demo = a;
			demo();
			
			// 2绑定操作,绑定某一个操作,不需要调用,该操作是网页打开自动预加载执行
			window.onload = function(){
				
			}			
		</script>
	</head>
	<body>
		
	</body>
</html>

Array

数组的功能和python中的一样,不多做介绍,直接定义使用
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		
	</body>
</html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>数组</title>
		
		<script type="text/javascript">
			// 1 直接定义
			var arr_list1 = ['1','2','3'];
			
			// 使用new方法创建
			var arr_list2 = new Array;
			// 使用循环给数组赋值
			for (var i=0;i<=5;i++){
				arr_list2[i] = window.prompt('输入数组的第'+i+'个值');
			}
			
			// 二维数组的定义(数组中套数组)即数组中的元素也是一个数组
			var arr_list3 = [['1','2','3'],['10','20']];
			
			// 数组的访问,通过下标索引访问
			document.write('一维数组第一个值'+arr_list1[0])
			document.write('二维数组第一个元素的第一个值'+arr_list3[0][0]);
			
			// 修改: 直接通过下标重新赋值
			arr_list3[0][1] = '99';
			
			document.write('修改之后的数组'+arr_list3);
			
		</script>
	</head>
	<body>
	</body>
</html>

Objects

  • Character objects: String character var str1 = 'abcd'
  • Target value: Number A value var number1 = 9527;
  • Boolean value object: boolean value true / false
  • Math object: a mathematical object in Javascript
  • function objects: that is, each function is an object
  • array objects: the array of objects
  • BOM objects
  • DOM objects

Creating an object method

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>对象</title>
		<script type="text/javascript">
			// 1、直接诶使用Object对象创建对象:对象的创建以及使用方法
			var js_obj = new Object;
			js_obj.name = 'html';
			js_obj.age = '999';
			js_obj.address = 'Beijing';
			
			// 给该对象定义方法
			js_obj.js_funt = function(){
				var str1 = '';
				str1 += '<h3>姓名:'+js_obj.name;
				str1 += '&nbsp;&nbsp;性别:'+js_obj.age;
				str1 += '&nbsp;&nbsp;地址:'+js_obj.address;
				 document.write(str1)
			}
			
			// 调用方法
			js_obj.js_funt()
			
			// 2、使用{}创建对象,在对象内部使用this关键字创建对象的方法
			var js_obj1 = {
				   name		: "韩信",
				   sex		: "男",
				   age		: 18,
				   js_funt1 : function(){
						//this是系统关键字,代表当前对象
						var str = "<h2>"+this.name+"的基本信息如下</h2>";
						str += "姓名:"+this.name;
						str += "<br>性别:"+this.sex;
						str += "<br>年龄:"+this.age;
						document.write(str);
				   },
				
			  };
			//调用对象方法
			js_obj1.js_funt1();
		</script>
	</head>
	<body>
		
	</body>
</html>

Published 63 original articles · won praise 1 · views 2037

Guess you like

Origin blog.csdn.net/qq_45061361/article/details/104380208