javascript類式繼承模式#3——借用和設置原型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>類式繼承模式#3——借用和設置原型</title>
</head>

<body>
<script type="text/javascript">

    function Parent(name){
        this.name=name||'Adam';
    };
    
    Parent.prototype.say=function(){
        console.log(this.name);
    };
    
    function Child(name){
        Parent.apply(this,arguments);
    };
    
    Child.prototype=new Parent();
    
    
    
/***************************************/

var kid=new Child('Janking');

console.log(kid.name);
kid.say();

//缺點:父構造函數被調用了兩次,所以致使了其效率低下的問題,最後,自身的屬性會被繼承兩次。


</script>
</body>
</html>
相關文章
相關標籤/搜索