• 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions


    明确函数的双重作用(Clarifying the Dual Purpose of Functions)

    在ES5及更早的ES版本中,函数调用时是否使用new会有不同的作用。当使用new时,函数内的this指向一个新对象并且函数会返回这个对象。看下面的代码:

    function Person(name) {
        this.name = name;
    }
    
    var person = new Person("Zakas");
    var notAPerson = Person("Zakas");
    
    console.log(person);  //  [Object object]
    console.log(notAPerson);  //  undefined

    一个自然的问题就是:如何判断函数调用时有没有使用new。在ES5中使用instanceof来判断:

    function Person(name) {
        if (this instanceof Person) {  
            this.name = name;
        } else {
            throw new Error("You must use new with Person");
        }
    }
    
    var person = new Person("Zakas");
    var notAPerson = Person("Zakas");  //  throws an error

    上面的代码会判断this是不是Person的实例,如果是那么继续执行,否则抛出错误。这个方法不可靠,因为即使不使用new,this也可以是Person的实例:

    function Person(name) {
        if (this instanceof Person) {  
            this.name = name;
        } else {
            throw new Error("You must use new with Person");
        }
    }
    
    var person = new Person("Zakas");
    var notAPerson = Person.call(person, "Zakas");  

     Person.call的第一个参数是person,它是一个Person实例。所以this也是一个Person实例。也就是说用instanceof没办法判断函数调用时是否使用new。为了解决这个问题,ES6中引入了new.traget:

    function Person(name) {
        if (typeof new.target !== "undefined") {  
            this.name = name;
        } else {
            throw new Error("You must use new with Person");
        }
    }
    
    var person = new Person("Zakas");
    var notAPerson = Person("Zakas");  //  throws an error

    当Person调用时使用new,new.target指向Person,上面的判断语句实际上可以写成new.traget === Person。再看一个例子:

    function Person(name) {
        if (typeof new.target !== "undefined") {  
            this.name = name;
        } else {
            throw new Error("You must use new with Person");
        }
    }
    
    function AnotherPerson(name) {
        Person.call(this, name);
    }
    
    var person = new Person("Zakas");
    var notAPerson = new AnotherPerson("Zakas");  //  throws an error

    上面的代码会报错,因为并没有在调用Person时使用new,new.traget并不指向Person。

    注意:new.target只能在函数内部使用,否则会报错的。可以看到,通过使用new.target我们可以判断函数调用时是否使用new。

  • 相关阅读:
    C# WPF之Material Design自定义颜色
    C# WPF从RIOT API获取数据(RIOT代表作品《英雄联盟》)
    C# WPF聊天界面(3/3)
    C# WPF简况(2/3)
    C# WPF联系人列表(1/3)
    使用SignalR从服务端主动推送警报日志到各种终端(桌面、移动、网页)
    为什么Node.JS会受到青睐?
    2017级面向对象程序设计——团队作业1
    2017级面向对象程序设计 作业三
    如果抽不出时间写博客怎么办
  • 原文地址:https://www.cnblogs.com/xfshen/p/5994293.html
Copyright © 2020-2023  润新知