zoukankan      html  css  js  c++  java
  • 关于js中函数的一点总结

    1函数中this作用域

    this根据当前环境来决定作用域,可以使用call和apply的方法来改变当前的this指向

        <script>
            var name = "global";
            function fn(){
                console.log(this.name);
            };
            fn();//函数申明式,this指向全局,"global"
            var obj ={
                name:"object",
                say:function(){
                    console.log(this.name);
                }
            }
            obj.say();//对象中调用,this指向当前对象,"object"
            function Person(){
                this.name="person";
                this.say=function(){
                    console.log(this.name);
                }
            }
            var p = new Person();
            p.say();//构造函数中调用指向当前构造函数,“person”
        </script>

    2函数的继承方法

    构造函数和函数原型的继承

    在构造函数里调用call,实现继承

        <script>
            function Parent(){
                this.name = "zzz";
                this.age = 233;
            };
            function Child(){
                Parent.call(this);
                this.favor = "dance";
            }
            var p = new Parent();
            var c = new Child();
            console.log(p);//{name: "zzz", age: 233}
            console.log(c);//{name: "zzz", age: 233, favor: "dance"}
        </script>

    原型链的继承

        <script>
            function Parent(){
                this.name = "zzz";
                this.age = 23;
            };
            function Child(){
                this.favor = "dance";
            }
            Parent.prototype.id=233;
            Child.prototype = new Parent();
            var c = new Child();
            console.log(c.id);//233
        </script>
  • 相关阅读:
    set的使用
    this.$watch(),this.$set(),this.$nextTick()=>{})
    web 错误代码解析
    samba 问题解决
    Linux postfix配置方法
    Linux rhcsa认证考试试题模拟
    Linux 使用nmcli配置网络
    Linux 链路聚合
    Linux ISCSI服务配置
    Linux Apache配置https访问
  • 原文地址:https://www.cnblogs.com/Zxq-zn/p/11479547.html
Copyright © 2011-2022 走看看