几道面试题

2017-05-11  本文已影响8人  去年17

1、通过原型扩展Array的distinct方法
经过好几种方式的最终还只有一种傻办法实现
其原理为,定义两个数组,一个为筛选过后的数组,一个为原数组
其代码如下

/ 方法一 新建一个临时数组进行接收,然后进行匹对返回
Array.prototype.distinct =
    function () {
        let arr = this; 
        let new_arr = [];
        if (arr.length == 0) {
            new_arr = arr;
        } else {
            arr.forEach((item, index) => {
                if (new_arr.indexOf(item) < 0) {
                    new_arr.push(item);
                }
            });
        }
        return new_arr;
    }
//for test
let arr = [4, 5, 6, 7, 4, 34, 7, 5, 6, 7, 56]
console.log(`原来数组 ${arr} \n输出数组${arr.distinct()} `);

小例子一个:

[4, 5, 6, 7, 4, 34, 7, 5, 6, 7, 56].sort((a,b)=>{return a-b})
//输出[4, 4, 5, 5, 6, 6, 7, 7, 7, 34, 56]

2、HTML 5 应用程序缓存(cache)参考

index.html

<!DOCTYPE HTML>
<html manifest="demo.appcache">

<body>
The content of the document......
</body>

</html>

demo.appcache

CACHE MANIFEST
# 2012-02-21 v1.0.0
/theme.css
/logo.gif
/main.js

NETWORK:
login.asp

FALLBACK:
/html5/ /404.html

其中demo.appcache是一个缓存描述文本文件(推荐以appcache为后缀)
manifest 文件可分为三个部分:
CACHE MANIFEST - 在此标题下列出的文件将在首次下载后进行缓存
NETWORK - 在此标题下列出的文件需要与服务器的连接,且不会被缓存
FALLBACK - 在此标题下列出的文件规定当页面无法访问时的回退页面(比如 404 页面)其中上面的 FALLBACK 小节规定如果无法建立因特网连接,则用 "/404.html" 替代 /html5/ 目 录中的所有文件

重要的提示:以 "#" 开头的是注释行,但也可满足其他用途。应用的缓存会在其 manifest 文件更改时被更新。如果您编辑了一幅图片,或者修改了一个 JavaScript 函数,这些改变都不会被重新缓存。更新注释行中的日期和版本号是一种使浏览器重新缓存文件的办法。

上一篇 下一篇

猜你喜欢

热点阅读