web前端核心JavaScript最基础的方法学习(一)

c## 一、JavaScript数据类型

基本数据类型

number (特殊NaN) NaN:not a number Infinity
boolean:true,false或者可以隐式转换在true或false的类型

能转换成false只有以下几种情况:0,空串,null,undinfed,NaN,false
string:用’'或"",或``定义的字符

var str='abcd'
var str2="xyz"
var str3=`hello,${str}`
 null:typeof null Object   undefined:定义未赋值和没有定义的变量类型都是undefined

null和undefined的区别:
http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html
Symbol:ES6新增的数据类型,用Symbole()函数来定义,代表定义的变量值的唯一性

引用数据类型(复杂数据类型)

Object,Array,Function,RegExp,String

对象类型可以添加属性 检测对象是否是对象自身的属性:hasOwnProperty

for(var key in obj) {

if(obj.hasOwnProperty(key)) {

  console.log(obj[key]) }

}

二、数组:Array

数组方法
栈方法:
push():尾部添加
pop:尾部删除
unshift:头部添加
shift:头部删除
splice:在数组任意的位置添加,删除和替换

  删除:splice(要删除的起始下标,删除数量)
  添加:arr.splice(要插入的起始位置,0,要添加的值)
  
      arr.splice(2,0,'全栈1909A')
      
  替换:arr.splice(要替换的志起始位置,替换的数量,替换的值)
  
      arr.splice(4,2,'我要去阿里')

数组内置的遍历方法:

forEach() :就是for的升级版,forEach返回值undeifned
filter():过滤,遍历满足条件的数组元素,返回新数组
map():对原数组加工处理,得到一个新数组
reduce():归并,将多个值归并成一个值
findIndex() 找下标
find() 找数组中匹配的元素
every() 全部满足才返回true,否则为false
some() 只要有一个满足就返回true,否则为false
includes() 判断数组中是否含有某个值,含有返回true,不含有返回false
flat() 扁平化 将多维数组转换成一维数组

  扁平化方法:
  
    1.toString实现
    
    例如:
    var arr=[5,5,5,5,[2,4,[2,[23,5],6],7],3,2,2,5]
    arr.toString().split(',').map(item=>item*1)
    
    2.用递归来实现数组的扁平化
    
    function flatFn(arr) {
      var result=[];
			if(!(arr instanceof Array))  return;
       for(var key in arr) {
         result=result.concat(Array.isArray(arr[key]) ? flatFn(arr[key]) : arr[key] 					)
         }
			  return result;
			}
			
		  3.flat()  //ES2019发布方法

Array参考文档:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

2.类数组:

– 何为类数组?
也称伪数组 LikeArray,只能通过length获取数量,和通过下标指定具体的某个元素,但不能使用数组的API方法

类数组的使用场景:获取的dom集合,arguments,…

– 如何将类数组转换为数组

1.Array.from(类数组)或 […类数组]
2.Array.prototype.slice.call(类数组)

ES1.0 1997
ES2.0 1998
ES3.0 1999
ES4.0 夭折
ES5.0 2009
ES5.1 2011
ES6 2015,由此开始,每年都会发布一个新版本
ES7 2016
ES8 2017

官方ECMA Script仓库更新地址https://github.com/tc39/proposals/blob/master/finished-proposals.md

三、字符串:String

trim() 去除字符串左右空格

不和trim,自己封装一个trim
function trim(str) {
var reg=/^\s+|\s+$/g
return str.replace(reg,’’)
}

String.prototype.quchukongge=function() {
var reg=/^\s+|\s+$/g
return this.replace(reg,’’)
}

toUpperCase() 字母转大写
toLowerCase() 字母转小写
substr() 取子串 str.substr(起始位置,取几个)
substring(起始下标,结束下标) 取子串
split() 字符串转数组 join:数组转字符串
slice(起始下标,结束下标) 取子串
replace(要查找的字符串或匹配的正则,要替换的内容) 替换
indexOf() 查找,有返回下标,没有返回-1
includes() 同数组用法 有返回true,没有返回false

四、数学对象(Math)

Math.abs() : 取绝对值

Math.random() 随机值 范围:0~~~~1

返回做任意数值范围 start-end范围

数量=y-x+1

x=10 ,y=20
11 num=y-x+1
数量=11
起始值:10
公式:Math.floor(Math.random()*数量)+起始值

封装了一个随机函数:
// start:起始下标
// end:开始下标
function randomIndex(start,end) {

var num=end-start+1

return Math.floor(Math.random()*num)+start
}

Math.foor() 向下取整

Math.ceil() 向上取整

Math.round() 四舍五入取整

Math.max() 取大值

var arr=[23,3,34,23,24,-32,34,54,234,234,235,534,235,2]

Math.max.apply(Math,arr)
Math.min.apply(Math,arr)
Math.min.call(Math,…arr)
Math.max.call(Math,…arr)

Math.min() 取小值

五、this指向

检测是否是手机端,是的话,跳转到移动页面

try {
    
    
  if (location.search.indexOf('?pc') !== 0 && /Android|Windows Phone|iPhone|iPod/i.test(navigator.userAgent)) {
    
    
    window.location.href = 'https://xw.qq.com?f=qqcom';
  }
} catch (e) {
    
    }
this的使用场景
*/
//1.直接调用函数 this即window
//console.log(this===window)

/*
function Fn() {

   console.log('Fn:',this)

   //this.name='1909A'
}
//Fn()

//2. new 函数,this即为当前构造函数实例化对象
//var f1=new Fn()

//3.函数属于对象

/*
var obj={
    name:'1909A',
    Fn:function() {

        var hehe=function() {

            console.log('this===window:',this===window)
             console.log('this===obj:',this===obj)
             //console.log('this===Fn:',this===Fn)
        }

       hehe()


    }

}



obj.Fn();

*/
//4.定时器上的this
/*
var obj={
    name:'1909A',
    Fn:function() {

       setTimeout(function() {

           console.log('this===window:',this===window)
           console.log('this===obj:',this===obj)

       },0)

    }

}

obj.Fn();

*

/

//5.DOM中的this

var btn=document.querySelector('#btn')
btn.onclick=function() {
    
    
  // let _this=this;
   setTimeout(()=> {
    
    

        console.log(this===btn)
   },0)

}

总结:this通常是谁调用,this指向谁,难道真的是这样吗??

var name='ok'

var obj={
    
    
   name:'alice',
   getName:function() {
    
    

     console.log(this.name)

  }
 
}

(false || obj.getName)()

欢迎大家进群进行技术性的探讨, 群号:954314851

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_48193717/article/details/108328437
今日推荐