- 正则表达式那块一定要搞懂
- 关于++i,i++,前自增是先进行自增运算,后自增是后面才来进行运算。
- js表单验证,搞几个demo。
4. 数组的增删改查,这个最重要了;!!!!
5. setCookie这里好好研究下
1 | <html> |
2 | <head> |
3 | <script type="text/javascript"> |
4 | function getCookie(c_name) |
5 | { |
6 | if (document.cookie.length>0) |
7 | { |
8 | c_start=document.cookie.indexOf(c_name + "=") |
9 | if (c_start!=-1) |
10 | { |
11 | c_start=c_start + c_name.length+1 |
12 | c_end=document.cookie.indexOf(";",c_start) |
13 | if (c_end==-1) c_end=document.cookie.length |
14 | return unescape(document.cookie.substring(c_start,c_end)) |
15 | } |
16 | } |
17 | return "" |
18 | } |
19 | |
20 | function setCookie(c_name,value,expiredays) |
21 | { |
22 | var exdate=new Date() |
23 | exdate.setDate(exdate.getDate()+expiredays) |
24 | document.cookie=c_name+ "=" +escape(value)+ |
25 | ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) |
26 | } |
27 | |
28 | function checkCookie() |
29 | { |
30 | username=getCookie('username') |
31 | if (username!=null && username!="") |
32 | {alert('Welcome again '+username+'!')} |
33 | else |
34 | { |
35 | username=prompt('Please enter your name:',"") |
36 | if (username!=null && username!="") |
37 | { |
38 | setCookie('username',username,365) |
39 | } |
40 | } |
41 | } |
42 | </script> |
43 | </head> |
44 | |
45 | <body onLoad="checkCookie()"> |
46 | </body> |
47 | </html> |
正则表达式
// var str = “aaa23345555ljljlj777777777”;
// var reg = /(\d)\1/gi;
// console.log(str.match(reg));
// var str = “Is this all tHere is ?”;
// var patt1 = /[a-h]/gi;
// console.log(str.match(patt1));
// var str = “Is this all tHere is ?”;
// var patt1 = /[A-z]/g;
// console.log(str.match(patt1));
// var str = “Is this all 555 tHere is / / \ _?”;
// var patt1 = /\0/g;
// console.log(str.match(patt1));
// var str = ‘80583600a’;
// var regexp = /^[1-9][0-9]{4,10}$/gim;
// if (regexp.test(str)) {
// alert(‘is’);
// } else {
// alert(‘no’);
// }
// var patt1 = /e/g;
// console.log(patt1.exec(‘som text sfsfe’));
// // OUTPUT:e
// var patt2 = new RegExp(‘ee’);
// console.log(patt2.exec(‘some text’));
// // OUTPUT:null
var patt1=new RegExp(“e”);
console.log(patt1.test(“The best things in life are free”)); // true
// 改变了检索模式
patt1.compile(“eee”);
console.log(patt1.test(“The best things in life are free”)); // false
var str = “Visit W3School!”
console.log(str.search(/W3School/))
// OUTPUT:6
var str=”1 plus 2 equal 3”
console.log(str.match(/\d+/g))
// // OUTPUT:1,2,3
var str2 = “Visit Microsoft!”
console.log(str2.replace(/Microsoft/, “W3School”));
var str3 = str2.replace(/Microsoft/, “W3School”);
// OUTPUT:Visit W3School!
数组
var colors = [“red”,”blue”,”green”];
console.log(colors.toString());
var bb = colors.toString();
var cc = colors.valueOf();
var d = new Date();
var dd = d.toLocaleString();
var d2 = colors.toLocaleString();
colors.toString()
// ①:toString()方法返回由数组中每个值的字符串形式拼接并且以逗号相隔的字符串
// ②:valueOf()方法返回的还是数组
// ③:toLocaleString()方法也会返回一个数组值以逗号相隔的字符串,但与toString()方法不同的是在返回日期对象时格式不同。
var arr = new Array();
var count = arr.push(“red”,”blue”);
console.log(“count=” + count);
var count2 = arr.push(“pink”,”orange”);
console.log(“count2=” + count2);
var arr2 = arr.pop();
console.log(arr2);
// ①:push()方法可以接受任意数量的参数,逐个添加到数组末尾,返回修改后数组的长度
// ②:pop()方法从数组末尾移除最后一项,返回被移除的项
console.log(“—————-“);
var colors2 = new Array();
var num = colors2.unshift(“red”,”green”);
console.log(num);
var item = colors2.shift();
console.log(item);
// ①:shift()方法移除数组的第一次项并返回该项
// ②:unshift()方法在数组前端添加任意项,并返回新数组的长度
// 由栈方法跟队列方法可知,在这两种方法中添加数组项的方法返回新数组的长度,
// 移除数组项的方法返回被移除项
console.log(“—————-“);
var values = [0,1,5,20,15];
values.reverse();
console.log(values);
function compare(value1, value2){
return value2 - value1; //降序,升序return value1 - value2
}
values.sort(compare);
console.log(values);
// ①:reverse()方法可以反转数组项的顺序
// ②:sort()方法对数组进行升序排序,
// 但sort()方法会调用每个数组项的toString()转型方法,所以sort()方法比较的是字符串,
// 所以为了能正确排序,要将一个排序函数作为参数传给sort()方法。
//——————————–
// var colors = [“red”,”green”,”blue”];
// var removed = colors.splice(0);
// console.log(colors);
// console.log(removed);
// var colors = [“red”,”green”,”blue”];
// var removed = colors.splice(1,0,”yellow”,”orange”);
// console.log(colors);
// console.log(removed);
// var colors = [“red”,”green”,”blue”];
// var removed = colors.splice(1,1,”yellow”);
// console.log(colors);
// console.log(removed);
// ①:concat()方法用于连接两个或多个数组,不改变现有数组,只是返回被连接数组的一个副本
// ②:slice()方法能基于当前数组中的一个或多个项创建一个数组,接受一个或两个参数,即返回项的开始跟结束位置
// ③:splice()方法,(返回数组)使用这种方法的方式有三种,如下:
// 若要删除的项数为0,则返回空数组;若不为0,则返回由被移除项所组成的数组
// 删除:可以删除任意数量的项,只需指定两个参数,要删除的第一项和要删除的项数
// 插入:可以向指定位置插入任意数量的项:只需指定三个参数,起始位置、0(要删除的项数)、要插入的项
// 替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定三个参数,起始位置、要删除的项数和要插入的任意数量的项
// var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
// console.log(numbers.indexOf(4));
// console.log(numbers.lastIndexOf(4));
// console.log(numbers.indexOf(4, 4));
// console.log(numbers.lastIndexOf(4, 4));
// var person = {
// name: “Nicholas”
// };
// var people = [{
// name: “Nicholas”
// }];
// var morePeople = [person];
// console.log(people.indexOf(person));
// console.log(morePeople.indexOf(person));
// console.log(morePeople)
// ECMAScript 5 为数组定义了五个迭代方法, 每种方法接受两个参数, 要在每一项上运行的函数跟运行该函数的作用对象–影响this的值( 可选), 而传入这些方法的函数要接收三个参数: 数组项的值, 该项在数组中的位置和数组对象本身。
// ①: every() 方法对数组中的每一项运行给定的函数, 如果该函数对一项都返回true, 则返回true。( 相当于逻辑与)
// ②: filter() 方法对数组中的每一项运行给定的函数, 返回该函数会返回true的项组成的数组。
// ③: forEach() 方法对数组中的每一项运行给定的函数, 没有返回值。
// ④: map() 方法对数组中的每一项运行给定的函数, 返回每次函数调用的结果组成的数组。
// ⑤: some() 方法对数组中的每一项运行给定的函数, 如果该函数对一项返回true, 则返回true。( 相当于逻辑或)
// var numbers = [1, 2, 3, 4, 5];
// var sum = numbers.reduceRight(function(pre,cur,index,array){
// return pre + cur;
// });
// console.log(sum);
// ECMAScript5新增了两个归并数组的方法: reduce() 和reduceRight() 方法, 这两个方法都会
// 迭代数组的所有项, 构建一个最终返回的值。 这两个方法接收两个参数: 一个在每一项上调用的函数和( 可选) 作为归并 基础的初始值。
// 而作为参数的函数接收四个参数: 前一个值、 当前值、 项的索引值、 数组对象, 并且这个函数的任何返回值都会作为该函数的第一个参数自动传给下一项。
reduceRight() 方法接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值
reduce() 方法对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值。
var total = [0, 1, 2, 3].reduce(function(sum, value) {
return sum + value;
}, 0);
// total is 6
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
}, []);
// flattened is [0, 1, 2, 3, 4, 5]
var arr = [1, 2, 3, 4, 5, 7];
console.log(arr.forEach(function(item,index,arr){
console.log(‘item=’ + item + ‘index=’ + index + ‘arr=’ + arr);
return item > 2;
}));
//查找相同的部分
console.log(nums);
var num1 = [1,2,3,4,5,7];
var num2 = [2,3,5];
var nums = num1.filter(function(aaa){
return num2.indexOf(aaa) >= 0;
});
console.log(nums)