十三 call / apply

2018-09-18  本文已影响0人  WIN_Inn

作用:改变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 );

上一篇下一篇

猜你喜欢

热点阅读