JS学习笔记03-初识继承

<!DOCTYPE HTML>
<html lang="en">
<head>
 <meta charset="UTF-8"> 
    <title>test</title>
    <style type="text/css">    
    </style>
    </head>
    
<body>
     <script type="text/javascript">
     // ◆prototype方式
     var Widget = function(){
        title = '';     
        this.getTitle=function(){
                return this.title;
        }
         this.setTitle=function(){
                this.title = arguments[0];
        } 
    }

    
    var Button = function(){}
    Button.prototype = new Widget();

    
    var Edit = function(){}
    Edit.prototype = new Widget(); 

    
    var btn = new Button();
    btn.setTitle('button title name');

    
    var edt = new Edit();
    edt.setTitle('edit title name');
//当前对象中找不到时,查找prototype原型链 console.log(edt.getTitle()); console.log(btn.getTitle()); console.log(btn.title+' '+edt.title); // ◆call+prototype方式 var Toolbar = function(){ Widget.call(this); Toolbar.prototype = new Widget(); this.setTitle(arguments[0]); } var toolbar = new Toolbar('toolbar title name');
console.log(toolbar.getTitle()
+' '+ toolbar.title);
console.log(btn.hasOwnProperty(
'title'));//true console.log(btn.hasOwnProperty('setTitle'));//false console.log(toolbar.hasOwnProperty('title'));//true console.log(toolbar.hasOwnProperty('setTitle'));//true </script> </body>

 

 

posted @ 2014-06-01 16:23  fuyunlong  阅读(131)  评论(0)    收藏  举报