JS入门到精通详解(5)

数组去重案例

function fnNoRepeatOfArray(arr){
    var list = [];
    arr.forEach(function(value){
        if(list.indexOf(value) === -1){
            list.push(value);
        }
    })
    return list;
}
function fn(arr){
    for(var i = 0,len = arr.length;i < len;i ++){
        for(var j = i + 1;j < len;j ++){
            if(arr[i] === arr[j]){
                arr.splice(j,1);
            }
        }
    }
    return arr;
}
function fn(arr){
    var list = [];
    arr.sort(function(a,b){return a - b;});
    arr.forEach(function(value,index){
        if(value !== arr[index + 1]){
            list.push(value);
        }
    })
    return list;
}

字符串常用方法

如何声明

  1. 字面量方式: '' ""

  1. 构造函数方式: new String()

属性

  1. length : 长度

方法 (查、替、截、转)(所有的方法都不影响原串)

  1. indexOf('字符串',start) : 查找子串在父串中第一次出现的下标位置,如果没有,则返回-1

var str = 'how do you do';
console.log(str.indexOf('do')); //4
console.log(str.indexOf('de')); //-1
console.log(str.indexOf('do'(找str这个元素),5(从第几个下标开始找))); //11
console.log(str.indexOf('do',4)); //4
  1. lastIndexOf('字符串',start) : 查找子串在父串中从右向左查找第一次出现的下标位置,如果没有找到,返回 -1

var str = 'how do you do';
console.log(str.indexOf('do')); //4
console.log(str.indexOf('de')); //-1
console.log(str.indexOf('do',5)); //11
console.log(str.indexOf('do',4)); //4

console.log(str.lastIndexOf('do')); //11
console.log(str.lastIndexOf('de')); //-1
console.log(str.lastIndexOf('do',5)); //4
console.log(str.lastIndexOf('do',4)); //4
  1. charAt(index) : 根据下标查找字符。

  1. charCodeAt(index) : 根据下标查找字符编码。

  1. replace(旧串,新串) : 替换字符串

//声明字符串
var str = 'how do you do';
console.log(str.replace('do','de')); //'how de you do'

// for(var i = 0;i < str.length;i ++){
//     str = str.replace('do','de');
// }

while(str.indexOf('do') !== -1){
    str = str.replace('do','de');
}
console.log(str);
  1. substring(start,end) : 从哪截取到哪,支持参数互换、不支持负数

  1. substr(start,length) :从哪截至多少个

  1. slice(start,end) :从哪截取到哪,不支持参数互换、支持负数

var str = 'how do you do';
console.log(str.substring(4)); //do you do
console.log(str.substr(4)); //do you do
console.log(str.slice(4));//do you do

console.log(str.substring(4,6)); //do(第四个截取到第六个)
console.log(str.substr(4,6)); //do you(第四个开始向后截取六个数)
console.log(str.slice(4,6));//do

console.log(str.substring(6,4)); //do
console.log(str.slice(6,4));//''


console.log(str.substring(-6,-4)); //''
console.log(str.slice(-6,-4));//'yo'
  1. toUpperCase() : 将小写转为大写字母

  1. toLowerCase() : 将大写转为小写字母

  1. split('切割符',length) : 将字符串切割为数组

var str = 'How Are You';
console.log(str.toUpperCase()); //HOW ARE YOU
console.log(str.toLowerCase()); //how are you
var arr = str.split(' ',2); //['How', 'Are']
console.log(arr);
  1. concat() : 合并字符串

var str = 'hello';
console.log(str.concat(' world')); // hello world
去空白
  1. trim() : 删除字符串两端空白

var str = '     a     b    ';
console.log('(' + str.trim() + ')');  //(a     b)
  1. trimStart() : 删除字符串左端空白

var str = '    a    b    ';
console.log('(' + str.trimStart() + ')');  //(a    b    )
  1. trimEnd() : 删除字符串右端空白

var str = '    a    b    ';
console.log('(' + str.trimEnd() + ')');  //(    a    b)

静态方法

  1. String.fromCharCode(编码) : 根据编码返回字符

Math对象(Math对象不能new!!!!)

  1. Math.PI 圆周率

  1. Math.abs() 求绝对值

  1. Math.round() 四舍五入

如果负数时, > 0.5 进一 <=0.5 舍去
console.log(Math.round(4.5)); //5
console.log(Math.round(4.4)); //4
console.log(Math.round(-4.5)); // -4
console.log(Math.round(-4.5000001)); // -5
console.log(Math.round(-4.4)); //-4
console.log(Math.round(-4.6)); //-5
  1. Math.ceil() 向上取整

console.log(Math.ceil(4.1)); //5
console.log(Math.ceil(4.9)); //5
console.log(Math.ceil(-4.1)); //-4
console.log(Math.ceil(-4.9)); // -4
  1. Math.floor() 向下取整

console.log(Math.floor(4.1)); //4
console.log(Math.floor(4.9)); //4
console.log(Math.floor(-4.1)); //-5
console.log(Math.floor(-4.9)); // -5
  1. Math.max() 最大值

  • 求数组中最大值: Math.max.apply(null,数组) Math.max( ... 数组名)

  • 求数组中最小值: Math.min.apply(null,数组) Math.min( ... 数组名)

var arr = [2,3,1,4,5,3,4];
console.log(Math.max(2,3,1,4,5,3,4));
console.log(Math.min(2,3,1,4,5,3,4));
console.log(Math.max.apply(null,arr));
console.log(Math.min.apply(null,arr));
  1. Math.pow(m,n) 求m的n次方

  1. Math.sqrt(n) 求n的开方

  1. Math.random() 随机数 0 <= n < 1

Math.floor(Math.random() * (max - min + 1) + min) 推荐
Math.round(Math.random() * (max - min) + min)
function random(min,max){
    if(min > max){
        var t = min;
        min = max;
        max = t;
    }
    return Math.floor(Math.random() * (max - min + 1) + min);
}

练习

  1. 实现猜数字游戏(1~10,三次机会)

  1. 封装一个随机颜色的函数(至少封装三种)

  1. 封装一个4位验证码的函数(包含数字大写字母小写字母)

    • **扁平化数组 如:[1,2,[3,[4,5],[6,7],8],9] [1,2,3,4,5,6,7,8,9]**面试题

计时器

  1. timer = setInterval(函数,毫秒数) : 间歇性计时器

  • clearInterval(timer) : 取消计时器

  1. timer = setTimeout(函数,毫秒数) : 一次性计时器、延时器、定时器

  • clearTimeout(timer);

练习

  1. 数码时钟

  1. 倒计时

Date对象

如何创建日期对象?

  1. var date = new Date();

  1. var date = new Date(2002,2,5); //0~11

  1. var date = new Date(2002,2,5,18,30,50); //0~11

  1. var date = new Date('2002-3-5'); //1~12

  1. var date = new Date('2002-3-5 18:30:50'); //1~12

  1. var date = new Date('2002/3/5'); //1~12

  1. var date = new Date('2002/3/5 18:30:50'); //1~12

如何访问日期对象中的信息?(括号里不需要写参数)

  1. date.getFullYear() //年

  1. date.getMonth() //月 0~11

  1. date.getDate() //日

  1. date.getDay() //星期

  1. date.getHours() 时

  1. date.getMinutes() 分

  1. date.getSeconds() 秒

  1. date.getMilliseconds() 毫秒

  1. date.getTime() 时间戳

如何设置日期对象中的信息?(括号里需要写参数)

  1. date.setFullYear() 年

  1. date.setMonth() 0~11

  1. date.setDate() 日

  1. date.setHours() 时

  1. date.setMinutes() 分

  1. date.setSeconds() 秒

  1. date.setMilliseconds() 毫秒

  1. date.setTime() 时间戳

如何以本地格式的字符串输出日期对象?

  1. date.toLocaleString() 本地格式的日期时间字符串

  1. date.toLocaleDateString() 本地格式的日期字符串

  1. date.toLocaleTimeString() 本地格式的时间字符串

练习

  1. 求出自己已生活了多少天零多少小时零多少分钟零多少秒钟?

  1. 写出距当前日期7天后的日期时间(注,使用日期对象的方法实现,不允许自己计算)

简单的代码异步执行机制

同步:按步骤执行
异步:同时进行
  1. 计算机程序执行分为同步执行,和异步执行:

所谓的异步执行,是一种特殊的程序的执行方式,常见的异步程序有:
定时器(setInterval),延时器(setTimeou),各种事件的绑定(onclick......),ajax请求
  1. 异步程序的执行过程

  1. 从第一行代码开始执行

  1. 同步程序开始执行

  1. 遇到异步程序了,暂时不执行,将异步程序暂时存储在“异步池”中

  1. 所有的同步程序执行完毕

  1. 开始执行“异步池”中的异步程序

  • 若有设定了时间的程序,就会先执行到点了的程序

  • 若有设定的时间是相同的程序,则依照书写顺序执行

setTimeout(function(){ 
    console.log('我是异步执行的程序1'); 
} , 2000);
setTimeout(function(){ 
    console.log('我是异步执行的程序2'); 
} , 1000);
console.log('我是同步执行的程序')

//结果依次是:
//我是同步执行的程序
//我是异步执行的程序2
//我是异步执行的程序1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值