JavaScript basics (Date, String class common methods, array creation and common methods, array iteration loop, js creation class, use prototype to extend method properties, window object and location, decode URL encoding

Common methods of Date class in js:

//创建Date对象
			var d = new Date();
			console.debug("获取年:",d.getFullYear(),
			"获取月:",d.getMonth()+1,"获取天:",d.getDate(),
			"获取小时:",d.getHours(),"获取分钟:",d.getMinutes(),
			"获取秒:",d.getSeconds());

Common methods of String class in js

String常用的方法:
				charAt()	返回在指定位置的字符。
				concat()	连接字符串。
				indexOf()	检索字符串。
				split()	把字符串分割为字符串数组。
				substr()	从起始索引号提取字符串中指定数目的字符。
				substring()	提取字符串中两个指定的索引号之间的字符。
				toLowerCase()	把字符串转换为小写。
				toUpperCase()	把字符串转换为大写。

There are 4 ways to create an array in js:

			//创建数组方式1
			var array1 = new Array();//创建一个空数组
			console.debug(array1);
			
			//创建数组方式2
			var array2 = new Array(5);//创建一个数组,长度为5的
			console.debug(array2);
			
			//创建数组方式3
			var array3 = new Array("张飞","古巨基","王天霸");//我创建一个数组,该数组里面有3个元素,这3个元素是"张飞","古巨基","王天霸"
			console.debug(array3);
			
			//创建数组方式4  简写创建(老师最推荐的,因为创建最简单)
			var array4 = ["A","B","C"];
			console.debug(array4);

Common methods of arrays

			/*	concat()	连接两个或更多的数组,并返回结果。
				join()	把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。
				pop()	删除并返回数组的最后一个元素
				push()	向数组的末尾添加一个或更多元素,并返回新的长度。(就相当于java中list集合中的add方法)
				reverse()	颠倒数组中元素的顺序。
				toString()	把数组转换为字符串,并返回结果。
				var arr3 = arr1.concat(arr2);*/

			console.debug("连接数组",arr3);//"苍苍","兰兰","香香","加藤宇","周杰伦","老b"
			//把arr3数组转为字符串,并且每个元素以-进行分割
			var str = arr3.join("-");
			console.debug(str);
			//删除并返回数组的最后一个元素
			var value = arr3.pop();
			console.debug("被删除的元素:",value,"现在数组的值:",arr3);
			//追加元素
			arr3.push("乔峰");
			console.debug("添加元素:",arr3);
			console.debug("颠倒数组中的顺序:",arr3.reverse());
			console.debug("把数组转为字符串,默认是以,进行分割每个元素:",arr3.toString());

Array iteration loop:

//创建空数组
			var arr1 = ["苍苍","兰兰","香香"];
			
			for(var i=0;i<arr1.length;i++){
				console.debug(arr1[i]);
			}

js create class:

/*
				在js中怎么去创建类呢?
				答:函数名首字母大写就表示一个类
			 */
			//创建Person类,拥有name,age  2个字段,sayHello一个函数
			function Person(name,age){
				//在js对象内部,添加属性需要this来添加
				this.name = name;
				this.age = age;
				this.sayHello = function(){
					alert(this.name+"   "+this.age);
				}
			}
			//创建对象(通过构造方法赋值)
			var p1 = new Person("张三",22);
			//添加属性
			p1.sex = true;
			console.debug(p1);
			//取对象中属性的值
			console.debug(p1.name,p1.age);
			//调用对象中的函数
			p1.sayHello();
			//在js中this,和java中的this一个意思,看当前函数正在被哪个对象所调用
			//谁调用我,我this就代表是谁

The prototype property extends the properties or methods of the js object

/*
			 * 
			 * js中prototype属性是用来扩展js对象的属性或者方法的
			        注意:每个类都拥有prototype属性
			        
			    扩展类的属性或者方法的语法结构:
			    	类型.prototype.属性/函数名 = 值
			 */
			//所有Date类型对应的对象,都拥有了getTimer方法
			Date.prototype.getTimer = function(){
				return this.getFullYear()+" 年 "
				+(this.getMonth()+1)+" 月 "+this.getDate()+" 日";
			}
			
			var d = new Date();
			console.debug(d.getTimer());
			
			var dd = new Date();
			console.debug(dd.getTimer());

window common methods

/*
			 	window常见方法:
			 			1.alert()弹出框      一般使用场景: 测试的时候
			 			2.setTimeout(fun,seconds);  在seconds时间后,执行指定的函数
			 * 			3.setInterval(fun,seconds); 每隔seconds时间执行一次fun函数
			 */
			//2s后执行指定的匿名函数(该定时器,只会执行一次)
			setTimeout(function(){
				console.debug("AAAAAAAAAAAAA");
			},2000);
			
			
			//每隔2s都会执行指定的匿名函数
			setInterval(function(){
				console.debug("循环执行")
			},2000);

location use

//location是window的一个成员,索引获取location对象,是可以直接省略的
			//location就是把链接地址封装成一个对象了,我们可以获取链接地址,也可以重新修改链接地址
			var l = location;
			//获取当前页面对应的url地址
			console.debug(l.href);
			//JS可以控制页面跳转
			location.href = "https://jd.com";//修改当前页面的url地址

URL address decoding and encoding:

/*
			 * encodeURI   浅编码
			 * decodeURI  浅解码
			 * encodeURIComponent 深编码(它会对你的特殊符号一并编码://?) 
			 */
			url = encodeURI(url);
			console.debug("编码后的效果:",url);
			url = decodeURI(url);
			console.debug("解码后的效果:",url);
			url = encodeURIComponent(url);//使用场景:有时候我们会把url地址作为参数进行传递
			console.debug("深度编码:",url);
			url = decodeURIComponent(url);
			console.debug("深度解码:",url);

System function parseInt () converts data to integer

			//parseInt() 把数据转为整数
			var s = "123";
			var num = parseInt(s);
			console.debug("把字符串转为整数:",num);
			s = "123.445";
			num = parseInt(s);
			console.debug("把字符串转为整数:",num);
			s = "13abc";
			num = parseInt(s);
			console.debug("把字符串转为整数:",num);
			s = "abc12";
			num = parseInt(s);
			console.debug("把字符串转为整数:",num);//NaN
			/*parseInt能把字符串转为整数,前提:必须以数字类型开始的字符串才能转*/

js iteration loop

/*
			 	1.普通for循环
			 		for(var i=0;i<数组.length;i++){
			 		}
			 	2.for in循环
			 		for(var k in 数组/对象){
			 		}
			 	for in和普通for循环的区别:
			 		for in可以循环迭代对象
			 */
			for(var i=0;i<arr.length;i++){
				console.debug(arr[i]);
			}
			console.debug("=============================");
			//当你循环数组的时候,k就表示索引     
			for(var k in arr){
				console.debug(k,arr[k]);
			}
			console.debug("=============================");
			function Person(name,age){
				this.name = name;
				this.age = age;
			}
			var p = new Person("张三",22);
			//当你循环迭代对象的时候  k表是属性值
			for(var k in p){
				console.debug(k,p[k]);//这种方式先不管  
			}
			/*
			取对象的值有2种方式:
					对象.属性 
					对象["属性"]
			*/
Published 23 original articles · Like1 · Visits 161

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/105569134