十三 call / apply

作用:改变this指向

区别:传参列表不同       // apply必须传数组

function Person ( name, age ) { 

        //this == obj

        this.name = name;

        this.age = age;

}

var person = new Person ( 'deng', 100) ;

var obj = {};

Person.call( obj, 'cheng', 300);   // obj{ name : 'cheng',  age : 300}


应用

function Person ( name, age, sex ){

           this.name = name;

            this.age = age;

            this.sex = sex;

}

function Student (name, age, sex, tel, grade ) {

            //var this = { }

            Person.call ( this, name, age, sex);

            this.tel = tel;

            this.grade = grade;            

}

var student = new Student ( 'sunny', 123, 'male',  139, 2017 );

你可能感兴趣的:(十三 call / apply)