<script> // let a = { // name : "懶羊羊", // fn : function(){ // console.log(this.name); // } // } // let b = a.fn; // b.fn(); 為空 // call、apply的作用 // 1、執行函式;2、改變this指向 3、可以傳引數 //call 傳參:第一個引數是this的指向, //后面的引數都是 對應b函式的形參 // let a = { // name : "懶羊羊", // fn : function(){ // console.log(this.name); // } // } // let b = a.fn; // b.apply(a,["美美的","美羊羊"]); // let a1 = { // name : "fei羊羊", // fn : function(){ // console.log(this.name); // } // } // let b1 = a1.fn; // b1.apply(a1,["美美的","美羊羊"]);
//3、bind //作用:可以改變this指向,2、可以傳入引數,3、不可以執行函式,回傳一個新函式
let a = { name : "懶羊羊", fn : function(adj, person){ console.log(this.name + adj + person); } } let b = a.fn; // b.bind(a,["美美的","美羊羊"]); //什么也沒有 let c = b.bind(a ,"美美的","美羊羊"); // b.bind(a,"美美的","美羊羊")(); // b.bind(a,"美美的")("美羊羊"); // b.bind(a)("美美的","美羊羊"); //可以減少一下重復引數 //方式一、二、三 c(); //區別:call和apply立馬執行函式 bind不能執行函式,回傳一個新函式 //相同:都能改變this指向 都可以傳引數 //寫一個原始碼 new的實作
// function Beatifulsheep(){ // this.name = name, // this.age = age, // this.sec = sex // } // Beatifulsheep.prototype.sing = function(){ // console.log(this.name + "說,我會唱歌"); // } // Beatifulsheep.prototype.cook = function(){ // console.log(this.name + "說,我會做飯"); // } // let myy = new Beatifulsheep("美羊羊","22","女"); // myy.sing(); // myy.cook();
//new的作用 // 1、生成一個實體物件 (并且把函式內部的thia 指向實體物件) //2、運行改建構式 初始化里面的屬性賦值 //3、把實體物件的原型鏈指向建構式的原型物件
//1、可以訪問建構式的屬性 //2、可以訪問原型上的屬性
//2、可以訪問原型上的屬性 // function Person(){ // this.name = ["1",["2"]]; // } // function child(){ // Person.call(this); // } // let cc = new child(); // cc.protety = Person.protety;
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/36900.html
標籤:JavaScript
