javascript面试题

2019-03-16  本文已影响0人  岚平果

数据类型

语句

for (var i=0; i < 10; i++) {
  setTimeout(function () {
    console.log(i);
  }, 1000)
}

内存泄漏

声明函数和变量,是会占用内存的。一般情况,内存会自动清楚垃圾,现在不用的变量。当有些变量无法被清楚的时候,称之为内存泄漏。

数组

var arr = [1,2,3,4,5,1,2,3]
var arr = [{name:'apple', price:9}, {name:'orange', price:2}, {name:'banner', price:5}]
var arr = [{name:'apple', price:9}, {name:'orange', price:2}, {name:'banner', price:5}]
1. push 向数值后面追加成员
2. pop 删除数值最后一位成员
3. unshift 向数值前面添加成员
4. shift 删除数值最前面一位成员
5. splice 添加或删除数组中的元素(最好用的方法)
6. sort 数值排序
7. reverse 反转数组
------------------------------------------------
8.forEach 遍历数值每个成员
9.map 数值映射,返回一个新数值,和原数组成员,一对一
10.filter 数组过滤 return true。返回我们需要的成员,组成一个新数组

Date类型

正则表达式

String

1、字符串截取有哪些方法
2、查找字符串位置方法,有哪些方法

Math和Number

1.写一个函数,返回10-30的随机数
2.Math.ceil(num)向上取整
3.Math.ceil(num)向下取整
4.Math.ceil(num)四舍五入

面向对象

函数

window.color = 'blue';
var obj = {
  color: 'red',
  sayColor(otherColor) {
    alert(this.color + otherColor);
  }
}
obj.sayColor('yellow');

obj.sayColor.call(this, 'yellow');

obj.sayColor.apply(this, ['yellow']); // 怎么记,applay和arr很像,参数是arr

var say = obj.sayColor.bind(this, 'yellow');
say();

BOM

location
history
navigator
document

document

getElementById、getElementsByTagName、getElementsByName、getElementsByClassName
let _width = document.documentElement.clientWidth || document.body.clientWidth
let _height = document.documentElement.clientHeight || document.body.clientHeight

事件

ajax

const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send(null);
xhr.onreadystatechange = () => {
  if (xhr.readyState==4 && xhr.status==200) {
    // ...
  }
}

JSON

XML类似html一样,处理比较麻烦,数据比较大。JSON处理数据更方便,数据更清晰,数据大小也更小

本地存储

  1. 数据与服务器之间的交互方式
    · cookie的数据会自动的传递到服务器,服务器端也可以写cookie到客户端
    · sessionStorage和localStorage不会自动把数据发给服务器,仅在本地保存。
上一篇 下一篇

猜你喜欢

热点阅读