js对象继承的实现的几种方式

来源:互联网 发布:免费的程序化软件 编辑:程序博客网 时间:2024/05/17 07:30

<!-- javascript实现继承的几种方式-->
<script type="text/javascript">
 
 /*
   //javascript的继承
   //1,对象冒出
   function Parent(username)
   {
   
     this.username = username;
  this.sayHello = function ()
  {
  
   alert(this.username); 
  
    }
 
   
   
   }
  
   function Child(username,password)
   {
    this.method = Parent;
    this.method(username);
    delete this.method;  
   
    this.password = password;
   
    this.sayWord = function ()
    {
      alert(this.password);
    }
   
   }
  
  
     var parent = new Parent("zhangsan");
  var child = new Child("lisi","1234");
 
   parent.sayHello();
  
   child.sayHello();
   child.sayWord();
  

  //  var obj= new Parent("zhangsan");
 //obj.sayHello();

   */

 
 
 
 /*
  //javascript的继承
   //2,Call方法方式 Funciton对象的方法
 
   function test(str)
   {
 
   alert(this.name + "," + str)
   
   }
  
    object = new Object();
 object.name = "aiguoliang";
 //test.call(object,"zhangsan");
 
 
 function Parent(username)
 {
   this.username = username;
  
   this.sayHello = function ()
   {
    alert(this.username);
   }
 }
 
 function Child(username,password)
 {
  
     Parent.call(this,username); 
  this.password = password;
  
  this.sayWord = function ()
  {
   
   alert(this.password);
   
     } 
  
    }
 
 var parent = new Parent("zhangsan");
 var child = new Child("list","123");
 
 parent.sayHello();
 
 child.sayWord();
 child.sayHello();
 
 */


/*
//javascript的继承
   //3,apply方法
  
   function Parent(username,obj)
   {
     this.username = username;
  this.obj = obj;
  
  this.sayHello = function ()
  {
    alert(this.username + this.obj);
   
     }
   
   }
  
 
 function Child(username,password)
 {    
 
       var arr = new Array();
      arr.push(username);
   arr.push(password);
    Parent.apply(this,arr);
    this.password = password;
   
    this.sayWord = function ()
    {
     alert(this.password);
    }
 }
 
 
 var parent = new Parent("zhangsan","987");
 var child = new Child("lisi","123");
  
   parent.sayHello();
  
   child.sayHello();
   child.sayWord();
   
 */
 
  
   /*
   //javascript的继承
   //4,原型链方式(prototype chain) 方式实现javaScript的继承
  
    function Parent()
 {
    }
 
 Parent.prototype.hello = "hello";
 Parent.prototype.sayHello = function ()
 {
  
   alert(this.hello);
    
    }
 
 function Child()
 {
  
    }
  
   Child.prototype= new Parent();
  
   Child.prototype.word = 'word';
   Child.prototype.sayWord = function ()
   {
    alert(this.word);
   }
  
   var child = new Child();
  
   child.sayHello();
   child.sayWord();
   */
  
  
  
   /*
    //javascript的继承
   //5.使用混合的方式实现javascript的继承
  
  
  function Parent(hello)
  {
    this.hello = hello;
  }
 
  Parent.prototype.sayHello = function ()
  {
 
     alert(this.hello);
   
  }
 
 
  function Child(hello,world)
  {
   Parent.call(this, hello);
  
   this.world = world;
  }
 
    Child.prototype = new Parent();
 
   Child.prototype.sayWorld = function ()
   {
      alert(this.world);
   }
  
   var child = new Child("zhangsan","123");
  
  
   child.sayHello();
   child.sayWorld()
   */
  
  
  

</script>

原创粉丝点击