常见前端面试题JS部分
2024-10-07 12:32:44
1.闭包
2.JS操作和获取cookie
//创建cookie
function setCookie(name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
if (expires instanceof Date) {
cookieText += '; expires=' + expires;
}
if (path) {
cookieText += '; expires=' + expires;
}
if (domain) {
cookieText += '; domain=' + domain;
}
if (secure) {
cookieText += '; secure';
}
document.cookie = cookieText;
} //获取cookie
function getCookie(name) {
var cookieName = encodeURIComponent(name) + '=';
var cookieStart = document.cookie.indexOf(cookieName);
var cookieValue = null;
if (cookieStart > -1) {
var cookieEnd = document.cookie.indexOf(';', cookieStart);
if (cookieEnd == -1) {
cookieEnd = document.cookie.length;
}
3.js数组去重
function arr1() {
var n = []; //一个新的临时数组
for (var i = 0; i < this.length; i++) //遍历当前数组
{
//如果当前数组的第i已经保存进了临时数组,那么跳过,
//否则把当前项push到临时数组里面
if (n.indexOf(this[i]) == -1) n.push(this[i]);
}
return n;
} function arr2(){
var n = {},r=[]; //n为hash表,r为临时数组
for(var i = 0; i < this.length; i++) //遍历当前数组
{
if (!n[this[i]]) //如果hash表中没有当前项
{
n[this[i]] = true; //存入hash表
r.push(this[i]); //把当前数组的当前项push到临时数组里面
}
}
return r;
} function arr3(){
var n = [this[0]]; //结果数组
for(var i = 1; i < this.length; i++) //从第二项开始遍历
{
//如果当前数组的第i项在当前数组中第一次出现的位置不是i,
//那么表示第i项是重复的,忽略掉。否则存入结果数组
if (this.indexOf(this[i]) == i) n.push(this[i]);
}
return n;
}
4.
最新文章
- SQL2005 表分区亲测
- Leetcode Candy
- CSS布局概述
- XGBoost参数
- 开源入侵检测系统OSSEC搭建之二:客户端安装
- asp.net Server.HtmlEncode和HtmlDecode
- 我的第一个QML Button的实现
- Java 面向对象(转)
- OC-Protocol实现业务代理
- Struts源码之OgnlValueStack
- charles模拟手机流量网速
- Python 爬虫 之 阅读呼叫转移(一)
- Python 隔离环境 virtualenv
- python3基础-set
- python作业高级FTP
- hdu 3015
- Pro * c Oracle 12c
- 【LeeCode88】Merge Sorted Array★
- C语言面试题5
- Delphi XE10.1 引用计数
热门文章
- nginx 重写
- thinkphp model
- hdu 6127 : Hard challenge (2017 多校第七场 1008)(计算几何)
- The list of list is modified unexpected, python
- 【Python】安装Python3,打印HelloWorld
- FastDFS整合nginx(三)
- 基于ELK Stack7.1.0构建多用户安全认证日志系统
- Android解析编译之后的所有文件(so,dex,xml,arsc)格式
- 【HDOJ6646】A + B = C(模拟)
- mac的jvm调优工具