首页 > 代码库 > JS学习笔记03-初识继承

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); </script> </body>