前端跳槽必备知识

2019-03-11  本文已影响0人  seporga

一、页面布局

CSS盒模型

box-sizing:content-box // 设置为标准模型(浏览器默认)
box-sizing:border-box // 设置为IE模型
var dom = document.getElementById('dom')
dom.style.width/height // 普适
dom.currentStyle.width/height // 仅IE浏览器支持
window.getComputedStyle(dom).width/height // Chrome、Firefox
dom.getBoundingClientRect().width/height/left/top // 获取元素相对viewport(视窗)左上角的位置

二、DOM事件

function(event){
  event.preventDefault() // 阻止事件默认行为
  event.stopPropagation() // 阻止冒泡,防止事件往上传递
  event.stopImmediatePropagation() // 同一个元素绑定了多个方法,这个方法可以阻止剩下未触发的事件
  event.currentTarget // 当前事件实际绑定的元素
  event.target // 事件代理时,实际触发事件的子元素,IE不支持
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>customEvent</title>
</head>
<style>
    .target {
        width: 200px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        color: white;
        background: red;
    }
</style>
<body>
    <div class="target" id="target">目标元素</div>
</body>
<script>
    // 事件捕获演示
    var target = document.getElementById('target')
    window.addEventListener('click', function () {
        console.log('window 捕获')
    }, true)

    document.addEventListener('click', function () {
        console.log('document 捕获')
    }, true)

    document.documentElement.addEventListener('click', function () {
        console.log('html 捕获')
    }, true)

    document.body.addEventListener('click', function () {
        console.log('body 捕获')
    }, true)

    target.addEventListener('click', function () {
        console.log('target 捕获')
    }, true)

    // 事件捕获演示
    var target = document.getElementById('target')
    window.addEventListener('click', function () {
        console.log('window 冒泡')
    }, false)

    document.addEventListener('click', function () {
        console.log('document 冒泡')
    }, false)

    document.documentElement.addEventListener('click', function () {
        console.log('html 冒泡')
    }, false)

    document.body.addEventListener('click', function () {
        console.log('body 冒泡')
    }, false)

    target.addEventListener('click', function () {
        console.log('====  华丽的分割线  ======')
        console.log('target 冒泡')
    }, false)

    // 需要抛出事件的地方
    var eve = new Event('myEvent')
    target.addEventListener('myEvent', function () {
        console.log('dispatch自定义事件')
    })
    setTimeout(function () { // 3s后触发事件
        target.dispatchEvent(eve) // 注意这里dispatchEvent的是事件对象eve,而不是事件myEvent
    }, 3000)
</script>
</html>

三、 HTTP协议

http协议是一种无连接、无状态的协议

四、原型链

// 第一种方式:字面量
var o1 = {name:'o1'}
var o2 = new Object({name:'o2'})
// 第二种方式:构造函数
function M(name){name:'03'}
var o3 = new M()
// 第三种方式:
var M = function(name){this.name=name}
var o3 = new M('o3')
// 第四种方式
var p = {name:'o4'}
var o4 = Object.create(p)
var M = function(name){this.name=name}
var o3 = new M('o3')
o3 instanceof M // true
o3 instanceof Object // false
// 因此,instanceof无法准确判断实例的直接构造函数
// 要准确判断,可以借助__proto__和constructor,例如:
o3.__proto__.constructor===M // true 
前端跳槽必备知识

五、面向对象

<!-- 面向对象 -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>OOP</title>
</head>
<body>
    <div>面向对象,请打开控制台</div>
    <script type="text/javascript">
        // ==== 类的声明 ==== 
        function Animal() {
            this.name = 'name'
        }
        // ==== ES6中的class声明类 ==== 
        class Animal2 {
            constructor() {
                this.name = 'name'
            }
        }
        console.log('==== 类的声明与实例化 ====')
        console.log('Animal', new Animal())
        console.log('Animal2', new Animal2())

        // ==== 借助构造函数实现继承(无法继承父类原型链上的属性和方法) ==== 
        function Parent1() {
            this.name = 'Parent1'
        }
        Parent1.prototype.p1 = 'p1'
        Parent1.prototype.say = function () { console.log('Say Hi~') }
        function Child1() {
            Parent1.call(this) // 用apply也可以,只是传参的方式不一样
            this.type = 'Child1'
        }
        console.log('==== 借助构造函数实现继承 ====')
        console.log('Child1', new Child1)
        console.log('Parent1', new Parent1)

        // ==== 借助原型链实现继承 ==== 
        function Parent2() {
            this.name = 'Parent2'
            this.arr = [1, 2, 3]
        }
        function Child2() {
            this.type = 'Child2'
        }
        Child2.prototype = new Parent2() // Parent2的实例赋值给Child2的prototype属性
        console.log('==== 借助原型链实现继承 ====')
        console.log('Child2的原型链上有了Parent2的属性和方法', new Child2()) // Child2的原型链上有了Parent2的属性和方法
        // 这种方法的缺点,实例共享父类的属性
        var s1 = new Child2()
        var s2 = new Child2()
        console.log('s1.arr', s1.arr)
        console.log('s2.arr', s2.arr)
        s1.arr.push('4') // 改变实例1的arr值
        // 改变实例1的arr值
        console.log('借助原型链实现继承缺点:实例共享父类的属性')
        console.log('s1.arr', s1.arr)
        console.log('s2.arr', s2.arr) // s2的值也被改变了

        // ==== 组合方式实现继承 ==== 
        function Parent3() {
            this.name = 'Parent3'
            this.arr = [1, 2, 3]
            console.log('Parent3被调用了')
        }
        console.log('==== 借助原型链实现继承,解决了构造函数和原型链的缺点。但是这个方法中,父类被调用了2次 ====')
        function Child3() {
            Parent3.call(this)
            this.type = 'Child3'
        }
        Child3.prototype = new Parent3()
        console.log('Child3', new Child3())

        // ==== 组合方式实现继承优化1,解决父类被调用两次问题 ==== 
        function Parent4() {
            this.name = 'Parent4'
            this.arr = [1, 2, 3]
            console.log('Parent4被调用了')
        }
        function Child4() {
            Parent4.call(this)
            this.type = 'Child4'
        }
        Child4.prototype = Parent4.prototype
        console.log('==== 组合方式实现继承优化1,解决父类被调用两次问题,但无法确定实例的直接构造函数了 =====')
        console.log('Child4', new Child4())

        // ==== 组合方式实现继承优化2,解决父类被调用两次问题 ==== 
        function Parent5() {
            this.name = 'Parent5'
            this.arr = [1, 2, 3]
            console.log('Parent5被调用了')
        }
        function Child5() {
            Parent5.call(this)
            this.type = 'Child5'
        }
        Child5.prototype = Object.create(Parent5.prototype) // Object.create只拷贝了原型链的部分,不包含新
        Child5.prototype.constructor = Child5
        console.log('==== 组合方式实现继承优化2,Object.create方法 =====')
        var s7 = new Child5()
        console.log('s7 instanceof Child5', s7 instanceof Child5)
        console.log('s7 instanceof Parent5', s7 instanceof Parent5)
        console.log('s7.__proto__.constructor === Child5', s7.__proto__.constructor === Child5)

    </script>
</body>
</html>

六、通信

window.hhh = function () {  // 定义全局函数名
   console.log('window.hhh')
}
var script = document.createElement('script') // 创建script标签
script.src = 'https://www.baidu.com?callback=hhh'  // 向服务端传递callback名
script.onload = script.onreadystatechange = function () { // 监听onload事件
  console.log('hahah')
  window.hhh() // 运行服务端返回的hhh函数,格式类似:hhh({data:xxx})
}
document.body.append(script) // append到body中,发起请求

// 运行结果:
// hahah
// window.hhh

2.Hash(地址中,"#"号后面的内容,hash值改变浏览器不刷新)
3.postMessage
4.WebSocket
5.CORS(参考资料 http://www.ruanyifeng.com/blog/2016/04/cors.html

七、安全

1.CSRF(Cross-site request forgery)跨站请求伪造
攻击原理:用户登录A网站,访问网站B,网站B伪造了一个指向A网站的GET请求并携带相关参数,此时浏览器会自动携带Cookie,A网站验证Cookie通过,并执行B网站伪造的GET请求。
防御措施:Token验证、Referer验证、隐藏令牌

2.XSS(Cross-site scripting)跨域脚本攻击
攻击原理:不需要登录,利用漏洞,执行恶意脚本。
防御措施:参考 http://www.imooc.com/learn/812

3.两者区别
XSS向页面注入脚本,在脚本中做想做的事情;CSRF利用接口漏洞,自动执行接口(一般要求用户已登录)

算法

参考资料:https://coding.imooc.com/class/315.html

八、一道经典的题目,考考大家对布局的掌握程度

三栏布局:


image.png

参考答案:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>layout</title>
</head>
<style>
    html * {
        padding: 0;
        margin: 0;
    }
    .layout {
        margin-top: 20px;
    }
    .layout article>div {
        min-height: 100px;
    }
</style>
<body>
    <section class="layout float">
        <style>
            .left {
                float: left;
                width: 300px;
                background: red;
            }

            .right {
                float: right;
                width: 300px;
                background: blue;
            }

            .center {
                background: green;
            }
        </style>
        <article class="left-right-center">
            <div class="left">300px</div>
            <div class="right">300px</div>
            <div class="center">
                <h1>浮动解决方案</h1>
                <p>这是内容</p>
                <p>这是内容</p>
                <p>这是内容</p>
            </div>
        </article>
    </section>
    <section class="layout absolute">
        <style>
            .layout.absolute .left-right-center>div {
                position: absolute;
            }

            .layout.absolute .left {
                left: 0;
                width: 300px;
                background: red;
            }

            .layout.absolute .center {
                left: 300px;
                right: 300px;
                background: green;
            }

            .layout.absolute .right {
                right: 0;
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-right-center">
            <div class="left">300px</div>
            <div class="right">300px</div>
            <div class="center">
                <h1>绝对定位解决方案</h1>
                <p>这是内容</p>
                <p>这是内容</p>
                <p>这是内容</p>
            </div>
        </article>
    </section>

    <section class="layout flexbox">
        <style>
            .layout.flexbox {
                margin-top: 140px;
            }

            .layout.flexbox .left-right-center {
                display: flex;
            }

            .layout.flexbox .left {
                width: 300px;
                background: red;
            }

            .layout.flexbox .center {
                flex: 1;
                right: 300px;
                background: green;
            }

            .layout.flexbox .right {
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-right-center">
            <div class="left">300px</div>
            <div class="center">
                <h1>flexBox解决方案</h1>
                <p>这是内容</p>
                <p>这是内容</p>
                <p>这是内容</p>
            </div>
            <div class="right">300px</div>
        </article>
    </section>

    <section class="layout table">
        <style>
            .layout.table .left-right-center {
                width: 100%;
                height: 100px;
                display: table;
                padding: 0;
                margin: 0;
            }

            .layout.table .left-right-center>div {
                display: table-cell;
            }

            .layout.table .left {
                width: 300px;
                background: red;
            }

            .layout.table .center {
                width: 100%;
                background: green;
            }

            .layout.table .right {
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-right-center">
            <div class="left">300px</div>
            <div class="center">
                <h1>表格布局解决方案</h1>
                <p>这是内容</p>
                <p>这是内容</p>
                <p>这是内容</p>
            </div>
            <div class="right">300px</div>
        </article>
    </section>


    <section class="layout grid">
        <style>
            .layout.grid .left-right-center {
                width: 100%;
                height: 100px;
                display: grid;
                grid-template-rows: 100px;
                grid-template-columns: 300px auto 300px;
            }

            .layout.grid .left {
                background: red;
            }

            .layout.grid .center {
                background: green;
            }

            .layout.grid .right {
                background: blue;
            }
        </style>
        <article class="left-right-center">
            <div class="left">300px</div>
            <div class="center">
                <h1>网格布局解决方案</h1>
                <p>这是内容</p>
                <p>这是内容</p>
                <p>这是内容</p>
            </div>
            <div class="right">300px</div>
        </article>
    </section>

</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读