call和apply
call 和 apply 是用來改變this指向的
call和apply都可以理解為是函數(shù)自身的方法
? ? ? ? 第一個參數(shù)代表了 接下來this所代表的對象
call和apply的區(qū)別?
使用call方法來傳參 一個一個的傳參
使用apply方法來傳參 需要傳一個數(shù)組 數(shù)組里面的第一個就對應了f函數(shù)里面的
? ? ? ? 第一個參數(shù)涡尘,數(shù)組里面的第二個就對應了f函數(shù)里面的
? ? ? ? 第二個參數(shù)
?console.log( f.call(obj1,'bmw','tangc')
console.log( f.apply(obj1,['bmw','tangc']) )
構(gòu)造函數(shù)綁定實現(xiàn)繼承
在子類的內(nèi)部調(diào)用父類的方法
? ? 通過call()或apply()方法
組合繼承
也叫偽經(jīng)典繼承
? ? ? ? 將原型鏈繼承和構(gòu)造函數(shù)繼承組合在一塊
? ? ? ? 原型鏈實現(xiàn)對原型屬性和方法的繼承
? ? ? ? 借用構(gòu)造函數(shù)實現(xiàn)對實例屬性的繼承
原型屬性和方法 */
? ? ? ? // Person.prototype.weight = '70kg';
? ? ? ? // Person.prototype.eat = function(){
? ? ? ? // ? ? console.log('我會吃飯');
? ? ? ? // }
? ? ? ? // function Student(){
? ? ? ? // ? ? /* 借用構(gòu)造函數(shù)實現(xiàn)對實例屬性的繼承 */
? ? ? ? // ? ? Person.call(this)
? ? ? ? // }
? ? ? ? // /* 原型鏈實現(xiàn)對原型屬性和方法的繼承 */
? ? ? ? // Student.prototype = new Person()
? ? ? ? // Student.prototype.contructor = Student;
? ? ? ? // let stu1 = new Student();
? ? ? ? // console.log(stu1);
? ? ? ? // console.log(stu1.weight);
? ? ? ? // stu1.eat();
拷貝繼承
把父對象的所有屬性和方法朱庆,拷貝進子對象?
將父對象的prototype對象中的屬性,拷貝給Child對象的prototype對象
父類 */
? ? ? ? // function Person(){}
? ? ? ? // Person.prototype.head = 1;
? ? ? ? // Person.prototype.foot = 2;
? ? ? ? // Person.prototype.eat = function (){
? ? ? ? // ? ? document.write('我會吃飯');
? ? ? ? // }
? ? ? ? // // console.log(Person.prototype);
? ? ? ? // /* 子類 */
? ? ? ? // function Student(){}
? ? ? ? // // console.log(Student.prototype);
? ? ? ? // // for(let key in Person.prototype){
? ? ? ? // // ? ? Student.prototype[key] = Person.prototype[key]
? ? ? ? // // }