前端学习day17:dom:阻止默认事件,事件的解绑,this指向,改变this指向,实现数组拼接,值类型和引用类型,值传递和引用传递,深浅拷贝,箭头函数,箭头函数中的this指向

目录

1.阻止默认事件

2.事件的解绑

3.this指向

4.改变this指向的三种方式

5.实现数组拼接的方式

6.值类型和引用类型

7.值传递和引用传递

8.深浅拷贝--只针对引用类型

9.箭头函数

10.箭头函数中this指向


1.阻止默认事件

        现在页面中一般都直接采用局部刷新,是通过ajax请求来实现局部更新

表单form本身就具备页面刷新的功能,为了不让页面刷新使form发起ajax请求,就要阻止默认事件

 <!-- 使用 百度搜索的接口 -->

    <!-- <form action="http://www.baidu.com/s">

        <input type="text" name="wd">

        <input type="submit" value="提交">

    </form> -->

1.1  event.preventDefault();

1.2   event.returnValue = false;

1.3

<!-- onsubmit  方法 返回值为false  可以阻止默认事件  但无法再发起网络请求了 -->

    <form action="" οnsubmit="return false">

        <input type="text" name="wd">

        <input type="submit" value="提交">

    </form>

1.4

区别 onsubmit中是否包含 return

    不包含return 并不阻止默认事件  因为onsubmit 并没有返回值

    包涵return就类似第3种写法

 <form action="" οnsubmit="return mycommit()">

        <input type="text" name="wd">

        <input type="submit" value="提交">

    </form>

 function mycommit() {

            console.log("必须 显示的返回 false 才可以阻止默认事件");

            return false;

        }

1.5

<!-- type 为 submit 类型 会触发提交事件 -->

    <!-- type 为 button 类型 不会触发提交事件  不会触发 test()-->

    <form action="" οnsubmit="test()">

        <input type="text" name="wd">

        <input type="button" value="提交" οnclick="test1()">

    </form>

1.6  button按钮也会触发 submit事件

  <form action="" οnsubmit="test()">

        <input type="text" name="wd">

        <button>提交按钮</button>

    </form>

2.事件的解绑

1.置空

2.removeEventListener

        添加监听器时的回调函数必须写成具名函数 否则无法进行解绑操作

  <button>点我</button>
    <script>
        var btn = document.querySelector('button');
        btn.onclick = function () {
            console.log("hello");
        }
        setTimeout(() => {
            // 1.置空
            btn.onclick = null;
            // 2.removeEventListener
            btn.removeEventListener("click", fn);
        }, 5000);
        var fn = function () {
            console.log("124");
        }
        // 添加监听器时的回调函数必须写成具名函数 否则无法进行解绑操作
        btn.addEventListener("click", fn);

3.this指向

3.1全局作用域下  this指向Windows对象

        // 1. 全局作用域下  this指向window对象
        console.log(this);//window
        // 声明的全局变量 默认做为window的属性
        var a = 10;
        console.log(this.a);//10
        console.log(window.a);//10
        // 在全局作用域下 尽量避免使用name这个名称
        // this window 可以省略
        console.log(this.b);// undefined 没有给该属性赋值 为undefined的
        function test() {
            console.log("测试一下",this);// window
        }

        window.test();//window
        this.test();//window
        test();//window

3.2指向当前方法的调用者

// 指向当前方法的调用者
        var obj = {
            name:"张三",
            age:20,
            play(){
                console.log(this.name + "去打篮球");// this -> obj
            }
        }
        obj.play();//张三去打篮球

3.3指向当前事件的绑定者

  var btn = document.querySelector('button');

        btn.onclick = function () {
            console.log(this);// this -> btn对象
        }

4.改变this指向的三种方式

         函数可以使用的三个方法

        1.call  2.apply  3.bind

 1.call 立即调用 并传递多个参数

        hello.call(btn,12,345);

 2.apply 应用    立即调用 传两个参数 并且第二个参数为数组对象

         hello.apply(btn,[1,2]);

3.bind  返回一个新函数 必须调用才会执行

        hello.bind(btn,1,2)();

5.实现数组拼接的方式

 var a1 = [1,2,3];

var a2 = [4,5,6];

        // 1.展开运算符

        // var a3 = [...a1,...a2];

        // console.log("a3 = ",a3);

        // 2.for 遍历

        // for (const key in a2) {

        //     a1.push(a2[key]);

        // }

        // console.log("a1 =",a1);

        // 3.concat 连接

        // var a3 = a1.concat(a2);

        // console.log(a3);

        // 4.apply

        a1.push.apply(a1,a2);

        console.log(a1);

6.值类型和引用类型

        数据结构  栈和堆

        https://blog.csdn.net/qq_44799466/article/details/106074212

        1.值类型 存储在栈的数据结构中的   速度快 效率高

        string number bool null undefined

        var a = 10;

        var b = "string";

        var c = true;

        2.引用类型 Array  Function Object

        obj 称为对象的引用(指针)    引用是存放在栈中的  而对象本身是存放在堆中

        如果需要修改对象的值  通过指针找到对象的内容地址  进行修改其值

        var obj = {

            name: "张三",

            age: 20

        }

        var arr = [1, 2, 3];

7.值传递和引用传递

在赋值 或 传参时 把实参赋予形参的过程  称为值传递 引用传递

值传递  把值copy一份赋值新的变量    a改变时  不影响b变量

        var a = 10;

        var b = a;

        a = 11;

        console.log(" b = ", b);// 10

        function test(num) {

            num = 100;

        }

        test(a);

        console.log("a  === ",a);// 11

引用传递

        新老指针指向同一个对象   修改任何一个 两者的都时相同的

        只拷贝指针  不拷贝对象的值

        var obj = {

            name:"lili",

            age:18

        }

        var obj2 = obj;

        obj.name = '莎莎';

        console.log("obj2 == ",obj2.name);//莎莎

8.深浅拷贝--只针对引用类型

1.浅拷贝  只拷贝指针 不拷贝对象的地址

      var obj1 = {

            name: "张三",

            age: 20

        }

        var obj2 = obj1;

        obj1.name = "李四";

        console.log("obj2 .name = ", obj2.name);//obj2 .name =  李四

        console.log(obj1 === obj2);//true

2.深拷贝

        把对象的地址(值)拷贝一份新的 赋予一个新的变量

        var obj = {

            name: "张三",

            age: 20,

            gf: {

                name: "小绿"

            }

        }

1.JSON中的方法

   可以实现多层的深拷贝

        var jsonobj = JSON.stringify(obj);

        var obj2 = JSON.parse(jsonobj);

        console.log(obj === obj2);//false

        console.log(obj.gf === obj2.gf);//false

2.自定义函数来实现深拷贝 (只拷贝一层)

        function deepObj(obj) {

            var nObj = {};

            for (const key in obj) {

                nObj[key] = obj[key];

            }

            return nObj;

        }

        var obj1 = {

            name: "张三",

            age: 20

        }

        var newObj1 = deepObj(obj1)

        console.log("--------------");

        console.log(newObj1 === obj1);//false

        //   拷贝数组

        function deepArray(arr) {

            var nArr = [];

            for (const value of arr) {

                nArr.push(value);

            }

            return nArr;

        }

        var array = [1, 2, 3, 4, 5];

        var newArray = deepArray(array);

        console.log("============");

        console.log(newArray === array);//false

9.箭头函数

结构:(参数列表)=>{函数体}

   // 1.无参

        var test = () => {

            console.log(123)

        }

        test();

        // 2.多参

        var test1 = (a, b) => {

            console.log(a, b);

        }

        test1(10, 20);

        // 3.有返回值 多个参数

        var test2 = (a, b) => {

            return a + b;

        }

        console.log(test2(10, 20));

        // 4.单个参数 有返回值

        var test3 = (a) => {

            return a + 100;

        }

        console.log(test3(100));

        // 可以简写

        // 一个参数 可以省略 括号

        // 如果有返回值 并且业务逻辑简单 可以省略{} 和 return 关键字

        var test4 = a => a + 100;

        console.log(test4(19));

10.箭头函数中this指向

        在箭头函数中,this永远指向其父作用域的对象

var btn = document.querySelector('button');
        btn.onclick = () => {
            console.log("this ,", this);// window
        }
        var obj = {
            name: "zhagnsan",
            age: 20,
            play: function () {
                setTimeout(() => {
                    console.log("第一个this = ", this); // obj
                }, 1000);

                setTimeout(function () {
                    console.log("第二个this = ", this); // window
                }, 1000);
            }
        }
        obj.play();

        var obj1 = {
            name: "zhagnsan",
            age: 20,
            play: () => {
                setTimeout(() => {
                    console.log("第一个this = ", this); // window
                }, 1000);

                setTimeout(function () {
                    console.log("第二个this = ", this); // window
                }, 1000);
            }
        }
        obj1.play();

改变this指向

        var obj2 = {

            name: "zhagnsan",

            age: 20,

            play: function () {

                // 改变this指向

                // 1.箭头函数

                setTimeout(() => {

                    console.log("改变后的 = ", this); // obj

                }, 1000);

                //    2. 定义临时变量 that

                var that = this;

                setTimeout(function () {

                    console.log("改变后的that = ", that);// obj

                }, 1000);

                // 3. bind  call  apply

                setTimeout(function(){

                    console.log("bind后 = ",this);

                }.bind(this),1000);//obj

                // 演变过程

                // 1.

                // var fn = function() {

                //     console.log(1111111111111111111111)

                // }

                // setTimeout(fn,100);

                // 2.

                // var fn = function() {

                //     console.log(1111111111111111111111)

                // }

                // var newFn = fn.bind();

                // setTimeout(newFn,100);

                // 3.

                // var newFn = function () {

                //     console.log(1111111111111111111111)

                // }.bind();

                // setTimeout(newFn, 100);

                // 4.

                // setTimeout(function () {

                //     console.log(1111111111111111111111)

                // }.bind(this), 100);

            }

        }

        obj2.play();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值