- 1.数据转为Boolean有哪几种情况为flase
- 2.如何判断数组包含某个值
- 3.break和continue之间的区别
- 4.如何对对象进行遍历
- 5.什么是跨域
- 6.如何解决跨域
- 7.什么是同步,什么是异步
- 8.什么是重载
- 9.json格式的字符串如何解析为json对象
- 10.如何识别数组
- 11.字符串哪些方法支持正则表达式
- 12.原生ajax如何实现
- 13.jquery-ajax的传参
- 14.axios的 传参
- 15.search的返回值
- 16match的返回值
- 17typeof能否识别数组,函数
- 18函数传的参数时不定参,还是定参,传递的参数装载在函数的哪个对象中
- 19split和join
- 20如何阻止a标签的跳转
- 21jquery中获取元素的宽高
- 22every,filter,find,findIndex的返回值
- 23什么是回到地狱
- 24.同源策略
- 25for… in和for…of的区别
1.数据转为Boolean有哪几种情况为flase
A.NaN
console.log(Boolean(NaN));//false
B.null
console.log(Boolean(null));//false
C.undefined
console.log(Boolean(undefined));//false
D.””
console.log(Boolean(""));//false
E.0
console.log(Boolean(0));//false
2.如何判断数组包含某个值
A.includes 查找数组中是否存在某个值(返回值为boolean值)
var arr=[1,2,3]console.log(arr.includes(4));//false
B.indexOf 当返回值为-1时,不存在,存在时,就返回下标的值
var arr=[1,2,3]console.log(arr.indexOf(1));//0console.log(arr.indexOf(4));//-1
3.break和continue之间的区别
A.break 达到某个条件后,跳出循环
for(var i=0;i<6;i++){console.log(i)if(i==3){break;}}console.log("hello");//0 1 2 3 hello
B.continue 跳过某个条件,继续循环
for(var i=0;i<6;i++){if(i==3){continue;}console.log(i); //0 1 2 4 5}
4.如何对对象进行遍历
for…in..
var obj={name:"li",age:12,sex:"男"}for(var item in obj){console.log(obj[item]);}
5.什么是跨域
跨域:协议,子域名,主域名,端口号有任意一个不同就是跨域
协议不同
https://www.baidu.com
http://www.baidu.com
子域名不同
https://www.baidu.com
https://aaa.baidu.com
主域名不同
https://www.baidu.com
https://www.baidu.top
端口号不同
https://www.baidu.com:8080
https://www.baidu.com:80
6.如何解决跨域
A.jsonp
使用时引入jQuery文件$.ajax({type:"method",url:"",dataType:"jsonp",jsonp:"callback",success:function(data){},error:function(xhr){console.log(xhr.status)}})
B.服务器的解决方案
C.js中script标签不受同源策略的影响也可以实现跨域
var script = document.createElement("script");script.src = "http://api.douban.com/v2/movie/subject/1764796?&callback=handleResponse";document.body.prepend(script);function handleResponse(res) {// 对response数据进行操作代码console.log(res)}
7.什么是同步,什么是异步
同步:客户端想服务器端发送请求的时候,用户不可以进行其他操作
//同步操作console.log(1);alert("lalala");console.log(2);
异步:客户端想服务器端发送请求的时候,用户可以进行其他操作
//异步操作,console.log(1);setTimeout(()=>{console.log("http");},1000)console.log(2);
8.什么是重载
根据参数的不同,动态决定调用哪个方法
js中没有重载,因为重复声明,下面的会覆盖上面的声明
<script>//js中没有重载,因为重复声明,下面的会覆盖上面的声明function go(a){console.log(a)}function go(a,b){console.log(a+b)}go(10);go(10,20)</script>
使用arguments模拟重载
function go(){if(arguments.length==1){console.log(arguments[0])}else{console.log(arguments[0]+arguments[1])}}go(10);go(10,20);
9.json格式的字符串如何解析为json对象
JSON.parse()
var txt=xhr.responseText;/* JSON.parse() 可以json格式的字符串,转换为json格式的数据 */var obj=JSON.parse(txt);
10.如何识别数组
Array.isArray(arr)
arr instanceof Array
var arr=[1,2];console.log(Array.isArray(arr));//trueconsole.log(arr instanceof Array);//true
11.字符串哪些方法支持正则表达式
A.replace 替换
var str="hello";var reg=/e/g;console.log(str.replace(reg,""));//hllo
B.match 以数组的形式返回匹配的值
var str="hello";var reg=/e/g;console.log(str.match(reg));//["e"]
C.split 将字符串分割成数组
var str="hello";var reg=/e/g;console.log(str.split(reg)); //["h", "llo"]
D.search 获取值得下标(也可以用来判断值是否存在)
var str="hello";var reg=/e/g;console.log(str.search(reg));//1
12.原生ajax如何实现
1.创建ajax核心对象
2.与服务器建立连接
3.发送请求
4.响应
<p id="app"></p><script>var app=document.getElementById("app");var url="http://192.168.4.18:8000/"/* ajax 如何使用ajax向后台获取数据*//* 1.创建ajax核心对象 */var xhr=new XMLHttpRequest();/* 2.与服务器建立连接 xhr.open(method,url,async) 分别是 请求的方式,地址,是否异步*/xhr.open("get",url,true);/* 3.发送请求 */xhr.send();/* 4.响应onreadystatechange 监听服务器的响应状态*/xhr.onreadystatechange=function(){/* xhr.status 服务器端响应的状态码 200 *//* xhr.readyState 服务器响应的进度 4 响应已经完成 */if(xhr.readyState==4 && xhr.status==200){var txt=xhr.responseText;/* JSON.parse() 可以json格式的字符串,转换为json格式的数据 */var obj=JSON.parse(txt);console.log(obj);app.innerHTML=obj.name;}}</script>
13.jquery-ajax的传参
$.ajax({url, 请求的url地址type:"get", 请求的方式data,datatype, 请求的数据类型success:res=>{ 成功的回调console.log(res);}})
14.axios的 传参
axios({url,method:"get",params:{问号后面的值}}).then(res=>{console.log(res);})
15.search的返回值
A.值存在,返回下标
B.值不存在,返回-1
var str="hello";console.log(str.search(e));//1console.log(str.search(a));//-1
16match的返回值
数组
var str="hello";console.log(str.match(e));//["e"]
17typeof能否识别数组,函数
A.不可以识别数组,返回值为object
var arr=[1,2];console.log(typeof arr);//object
B.可以识别函数,返回值为function
function go(){}console.log(typeof go);//function
18函数传的参数时不定参,还是定参,传递的参数装载在函数的哪个对象中
函数传的是不定参,传递的参数装载在arguments对象中
<script>/*js传递不定参数,函数内部有一个arguments对象,接受传递过来的参数,arguments对象是一个类数组对象*/function go(a){console.log(arguments)console.log(a)}go(10,3);</script>
19split和join
A.split 将字符串分割成数组
var str="hello";var arr=str.split("");console.log(arr);//["h", "e", "l", "l", "o"];console.log(str.split());//["hello"];console.log(str.split("e"));//["h", "llo"];
B.join 将数组拼接成字符串
var arr=["html","css","js"];var b=arr.join();var c=arr.join("");var d=arr.join("/");console.log(b);//html,css,jsconsole.log(c);//htmlcssjsconsole.log(d); //html/css/jsconsole.log(typeof b);
20如何阻止a标签的跳转
return false
<a id="app" href="www.baidu.com">百度</a>let app = document.getElementById("app");app.onclick = function(){return false;}
21jquery中获取元素的宽高
<style>div{width: 200px;height: 200px;padding: 20px;border:1px solid #eee}</style>
A.content:width() height()
console.log($("div").width());//200console.log($("div").height());//200
B.content+pading innerWidth() innerHeight()
console.log($("div").innerWidth());//240console.log($("div").innerHeight());//240
C.content+padding+border outerWidth() outerHeight()
console.log($("div").outerWidth());//242console.log($("div").outerHeight());//242
22every,filter,find,findIndex的返回值
A.every 返回值是boolean值 只有数组中每一项满足某个条件,就输出true
var arr=[2,4,6];console.log(arr.every(item=>item>=2));//trueconsole.log(arr.every(item=>item>=4));//false
B.filter 返回值是以数组形式的匹配的值
var arr=[2,4,6];console.log(arr.filter(item=>item>=2));//[2,4,6]console.log(arr.filter(item=>item>=4));//[4,6]
C.find 返回值是通过测试(函数内判断)的数组的第一个元素的值
var arr=[2,4,6];console.log(arr.find(item=>item>=2));//2console.log(arr.find(item=>item>=4));//4
D.findIndex 返回值是通过测试的数组的第一个元素的下标
var arr=[2,4,6];console.log(arr.findIndex(item=>item>=2));//0console.log(arr.findIndex(item=>item>=4));//1console.log(arr.findIndex(item=>item>=7));//-1
23什么是回到地狱
24.同源策略
25for… in和for…of的区别
A.for…in
返回值是数据结构的键名
遍历对象返回值是对象的key值,遍历数组返回值是数组的下标
B.for…of
返回值是键值对中的值
