zoukankan      html  css  js  c++  java
  • javascript函数中的三个技巧【一】

    在学习javascript中,函数是非常重要的,现在我来谈谈对函数的理解以及在工作和用法中的一些技巧

    技巧一、

    【作用域安全的构造函数】

      构造函数其实就是一个使用new操作调用的函数

    function Person(name,age,job){
        this.name=name;
        this.age=age;
        this.job=job;
    }
    var person=new Person('match',28,'Software Engineer');
    console.log(person.name);//match

    如果没有new操作,原本针对Person对象的三个属性被添加到window对象中去

    function Person(name,age,job){
        this.name=name;
        this.age=age;
        this.job=job;
    }          
    var person=Person('match',28,'Software Engineer');
    console.log(person);//undefined
    console.log(window.name);//match

      如上代码所示,直接的person是一个undefined,然而Person中的属性都添加到window对象中

      window的name属性是用来标识链接目标和框架的,这里对该属性的偶然覆盖可能会导致页面上的其他错误,这个问题的解决方式就是创建一个作用域安全的构造函数

    function Person(name,age,job){
        if(this instanceof Person){
            this.name=name;
            this.age=age;
            this.job=job;
        }else{
            return new Person(name,age,job);
        }
    }
    var person=Person('match',28,'Software Engineer');
    console.log(window.name); // ""
    console.log(person.name); //'match'
    var person= new Person('match',28,'Software Engineer');
    console.log(window.name); // ""
    console.log(person.name); //'match'

      但是对构造函数窃取模式的继承,会带来副作用,这是因为,下列代码中,this对象并非Polygon对象实例,所以构造函数Polygon()会创建一并返回一个新的实例

    function Polygon(sides){
        if(this instanceof Polygon){
            this.sides=sides;
            this.getArea=function(){
                return 0;
            }
        }else{
            return new Polygon(sides);
        }
    }
    function  Rectangle(wifth,height){
        Polygon.call(this,2);
        this.width=this.width;
        this.height=height;
        this.getArea=function(){
            return this.width * this.height;
        };
    }
    var rect= new Rectangle(5,10);
    console.log(rect.sides); //undefined

      如果要使用作用域安全的构造函数窃取模式的话,需要结合原型链继承,重写Rectangle的prototype属性,使它的实例也变成Polygon的实例

    function Polygon(sides){
        if(this instanceof Polygon){
            this.sides=sides;
            this.getArea=function(){
                return 0;
            }
        }else{
            return new Polygon(sides);
        }
    }
    function  Rectangle(wifth,height){
        Polygon.call(this,2);
        this.width=this.width;
        this.height=height;
        this.getArea=function(){
            return this.width * this.height;
        };
    }
    Rectangle.prototype= new Polygon();
    var rect= new Rectangle(5,10);
    console.log(rect.sides); //2

    以上就是作用域安全性的一些理解  !接下来几篇文章就来讲解下对惰性载入函数

  • 相关阅读:
    session
    CSS3盒子模型
    由“从按下回车到网页显示”粗谈网页优化
    springMVC之拦截器
    设置Webdriver启动chrome为默认用户的配置信息
    [Swift]LeetCode498. 对角线遍历 | Diagonal Traverse
    [Swift]LeetCode497. 非重叠矩形中的随机点 | Random Point in Non-overlapping Rectangles
    [Swift]通天遁地Swift
    [Swift]LeetCode996. 正方形数组的数目 | Number of Squareful Arrays
    [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips
  • 原文地址:https://www.cnblogs.com/kester/p/6227561.html
Copyright © 2011-2022 走看看