2012-04-13 98 views
0

這裏我的問題,我不明白爲什麼這個覆蓋不要在這裏工作是源覆蓋方法在JavaScript

window.onload=function() 
{ 
    function Person(first,last) 
    { 
     this.First=first; 
     this.Last = last; 
     this.Full=function() 
     { 
     return this.First+" "+this.Last; 
     }; 
    } 

    Person.prototype.GetFullName=function() 
    { 
     return this.First + " " + this.Last; 
    } ; 



    function Employee(first,last,position) 
    { 
     Person.call(this,first,last); 
     this.Position=position; 
    } 
    /* Employee.prototype = new Person(); 
    var t = new Employee("Mohamed","Zaki","Web Developer"); 

    alert(t.GetFullName()); 
    */ 
    Employee.prototype.GetFullName=function() 
    { 
     var x = Person.prototype.GetFullName.call(this); 
     return x + " , " + this.Position ; 
    } 
    var e = new Employee("Mohamed","Zaki","Web Developer"); 
    alert(e.GetFullName()); 
    } 
+0

當我運行這個代碼(在添加了最後一個'}'後),它會發出警告'Web開發人員Mohamed Zaki。你在期待什麼? – 2012-04-13 04:02:23

+0

我知道代碼正在工作,但我在評論中詢問代碼,爲什麼它不起作用? – tito11 2012-04-13 04:10:05

+0

,因爲即使你設置了'this.Position = position',什麼都不讀。你期待它在GetFullName中嗎?也許你打算在評論中包含更多的代碼? – 2012-04-13 04:16:38

回答

1

如果我理解你的問題,你已經註釋掉不代碼因爲它在GetFullName被覆蓋之前執行。

/* 
    **** this code is executed before GetFullName is overridden and will use 
    **** original function 
    Employee.prototype = new Person(); 
    var t = new Employee("Mohamed","Zaki","Web Developer"); 

    alert(t.GetFullName()); 
    */ 

    Employee.prototype.GetFullName=function() 
    { 
     var x = Person.prototype.GetFullName.call(this); 
     return x + " , " + this.Position ; 
    } 

    /**** This code is executed after GetFullName is overridden uses the new version */ 
    var e = new Employee("Mohamed","Zaki","Web Developer"); 
    alert(e.GetFullName()); 
    } 
1

首先,刪除window.onload包裝器,因爲它沒有做任何有用的事情。說明如下: