JS不高效代码示例(参数的传递)

    function Person1(firstName, lastName) {//无法推断参数的含义
        this.firstName = firstName;
        this.lastName = lastName;
        this.toString = function() {
            return this.firstName + " " + this.lastName;
        }
    }

    function Person2(attrs) {//可读性更好,可扩展性更好
        this.firstName = attrs["firstName"]
        this.lastName = attrs["lastName"];
        this.toString = function() {
            return this.firstName + " " + this.lastName;
        }
    }
    var p1 = new Person1("Frank", "Zammetti");
    var p2 = new Person2({
        "firstName" : "Frank",
        "lastName" : "Zammetti"
    });
    alert(p1);
    alert(p2);

 

posted @ 2016-04-17 09:03  绯乐  阅读(223)  评论(0编辑  收藏  举报