javascript建構函式.prototype原型理解 函數.call() 方法複製的理解

來源:互聯網
上載者:User

標籤:typeerror   後台   ons   複雜   設計   logs   存在   this   delete   

 

直接建立object對象

var stu = new Object();或var stu = {};stu.name="zhangsan";stu.age = "18";stu.fun = function(){  alert("我叫張三");  }

你也可以用講stu當作一個模組,模組裡面有多個對象,如下執行個體為一個簡單的購物車系統的模組!
var shoppingCar = {}
shoppingCar.carInfo = function(obj){
shoppingCar.carInfo.carNumber = obj.carNumber;
.....
}//購物車對象
shoppingCar.orderInfo = function(obj){
shoppongCar.orderInfo.orderNumber = obj.orderNumber;
...
}//訂單對象

//調用
carInfo = {
  carNumber : "10000"
}
var car = new
shoppingCar.carInfo(carInfo);

  //prototype是什麼含義?

 

  //javascript中的每個對象都有prototype屬性,Javascript中對象的prototype屬性的解釋是:返回物件類型原型的引用。

  //A.prototype = new B();

  //理解prototype不應把它和繼承混淆。A的prototype為B的一個執行個體,可以理解A將B中的方法和屬性全部複製了一遍。A能使用B的方法和屬性。這裡強調的是複製而不是繼承。可以出現這種情況:A的prototype是B的執行個體,同時B的prototype也是A的執行個體。

已下是別人的筆記,挺有用的。

1.原型鏈中的參考型別。 
2.原型鏈不要互相依賴,GC可沒那麼好心幫你建立一個臨時儲存地區。

1 function base () {
this.arr = [];
} 
function sub (){ 

} 
sub.prototype= new base(); 
var a = new sub(); 
var b = new sub(); 
console.log(b.arr.length); //0
a.arr[0]=‘moersing‘; 
console.log(b.arr.length); //1

可以看出,arr是一個共用的,如果說不適用this.arr而用 var arr = [] 的話,sub訪問不到,只能通過父類的閉包來訪問,但是問題還是存在的。如果想繼承一個完全獨立的參考型別: 
第一 : 
function base () {
this.arr = [];
} 
function sub (){ 
base.call(this);// 先加上一個執行個體屬性    base.call(this)中 this指的是調用base方法的對象,即為window對象,call()方法是改變調用函數的對像
}                      // 例如function a(){}  預設調用a函數為window對象,function b(){ this.name="1"} 中this指代的為window a.call(b) 則將意味著b為調用a的對象
sub.prototype= new base();    //這句話本人理解為可以去除,沒有必要。。。。。。。然後就沒有下面delete之後還佔記憶體的說法了- -。
var a = new sub(); 
var b = new sub(); 
console.log(b.arr.length); //0
a.arr[0]=‘moersing‘; 
console.log(b.arr.length); //0 
console.log(a.arr.length); //修改了a,b不受影響 
這個問題可以解決,但是不完美,可以看出, a和b有一個執行個體屬性arr和一個prototype的arr,也就是說。 
a.arr[0] = ‘moersing‘ ; 
console.log(a.arr.length); //1 
delete a.arr; //把屬性執行個體幹掉 
console.log(a.arr.length);// 這個時候訪問的是 prototype的。 
所以,理論上來講,這並不能算是完美的,變數也是需要記憶體的不是嗎?好OK,那麼,接下來,使用另一種,上面那個叫 借用繼承。 
接下來這種方式比較完美,但是也不是那麼複雜,主要是使用借用繼承的思想罷了,也就是說,借用執行個體和借用原型分開。 
function base() {
this.arr = [];
}
base.Constructor = function () { //添加一個原型建構函式
//這裡判斷一下,如果是一個函數,並且不是一個Regex對象,IE7會把正則反映成一個function而不是一個object
if (typeof this === ‘function‘ && typeof this.prototype.source === ‘undefined‘) {
var fn = this.prototype;
fn.name = ‘moersing‘;
fn.age = ‘18‘;
fn.getFull = function () {
console.log(this.name + ‘|‘ + this.age);
};
}
else {
throw new TypeError(‘this is not a function‘);
}
};
function sub() {
base.call(this); //借用建構函式,執行個體化一個執行個體屬性
}
base.Constructor.call(sub); //再調用父類的方法,構造出一個prototype的。
var a = new sub();
var b = new sub();
a.arr[0] = ‘moersing‘; //給a執行個體的參考型別添加一個元素
console.log(a.arr.length); //結果是1
console.log(b.arr.length); //結果是0,也就是沒有收到影響
console.log(a.name); //列印a.prototype的屬性
console.log(b.name); //列印b.prototype的屬性
b.name = ‘linfo‘; //修改b的。
console.log(b.name); //linfo
console.log(a.name); //a沒有影響,還是moersing
a.getFull(); //moerisng|18
b.getFull(); //linfo |18

 

 

以下下是原型設計的建構函式,其中沒有像上面一樣在函數中執行個體Obejct對象,後台會幫你建立Object對象,this就代表了這個對象,對其操作。

function student(name,age){this.name = name;this.age = age;this.fn = function(){    alert("原型設計建構函式");}}

 

以下是寄生模式的建構函式,一般不建議使用,不能夠實現資料共用。

1 function student(name,class){2   var obj=new Object();3   obj.name = name  ;4   obj.class = class;  5 }6 var stu = new student("航點點","1班");

 

javascript建構函式.prototype原型理解 函數.call() 方法複製的理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.