zoukankan      html  css  js  c++  java
  • 第四章 JavaScript对象及初始面向对象

    创建对象:   

      //方式一
      var ower=new Object();
      ower.name="长春花";
      ower.genera="夹竹挑科 长春花属";
      ower.area="非洲,亚热带,热带,以及中国大陆的华东,西南,中南等地";
      ower.uses="观赏或用药等";
      ower.showName=function(){
         alert(this.name);
      }
      ower.showName();
     
      //方式二
      var flower={
         name:"长春花",
         genera:"夹竹挑花 长春花属",
         area:"非洲,亚热带,热带以及中国大陆的华东,西南,中南等地",
       uses:"观赏或用药等",
          showName:function(){
         alert(this.uses);
        }
      }
      flower.showName();

    构造函数: 

      function Flower(name,genera,area,uses){
         this.name=name;
         this.genera=genera;
         this.area=area;
         this.uses=uses;
         this.showName=showName;
      }
      function showName(){
         alert(this.name);
      }
      var flower1=new Flower("长春花","夹竹挑科 长春花属", "亚洲 亚热带 热带以及中国的华东 西南 中南等地","观赏过用药等");
      var flower2=new Flower("牡丹","夹竹挑科 长春花属","亚洲 亚热带 热带以及中国的华东 西南 中南等地","观赏过用药等");
      var flower3=new Flower("百合","夹竹挑科 长春花属", "亚洲 亚热带 热带以及中国的华东 西南 中南等地","观赏过用药等");
      
      flower1.showName();
      flower2.showName();
      flower3.showName();
      //true
      alert(flower1.showName==flower2.showName);  //构造
      alert(flower1.constructor==Flower);
      alert(flower2.constructor==Flower);
      alert(flower3.constructor==Flower); 
      //验证
      alert(flower1 instanceof Object);  //类型
      alert(flower1 instanceof Flower);
      alert(flower2 instanceof Object);
      alert(flower2 instanceof Flower);
      alert(flower3 instanceof Object);
      alert(flower3 instanceof Flower); 
    原型对象:
      function Flower(){
      }
      //prototype是一个指针,指向了Flower对象
      Flower.prototype.name="百合";
      Flower.prototype.genera="茄科 属性";
      Flower.prototype.areas="非洲 亚热带 热带以及中国大陆的华东 西南";
      Flower.prototype.uses="观赏过或用药等";
      Flower.prototype.showName=function(){
         alert(this.name);
      }
      var flower1=new Flower();
      flower1.showName();
      var flower2=new Flower();
      flower2.showName();
      alert(flower1.showName==flower2.showName); 
    原型链:
      function Humans(){
         this.foot=2;
      }
      Humans.prototype.getFoot=function(){
         return this.foot;
      }
      function Man(){
         this.head=1;
      }
      Man.prototype=new Humans(); //继承了Humans()
      Man.prototype.getHead=function(){
         return this.head;
      }
      var man1=new Man();
      alert(man1.getFoot());
      alert(man1.getHead());
      alert(man1 instanceof Object);
      alert(man1 instanceof Humans);
      alert(man1 instanceof Man);
    对象继承:
      function Humans(){
         this.clothing=["trousers","dress","jacket"];
      }
      function Man(){
     
      }
      //继承了Humans
      Man.prototype=new Humans();
      var man1=new Man();
      //添加了一个coat的值
      man1.clothing.push("coat");
      alert(man1.clothing);
      var man2=new Man();
      alert(man2.clothing);
    借用构造函数:
      //方式一      
      function Humans(){
         this.clothing=["trousers","dress","jaclet"];
      }
      function Man(){
          Humans.call(this); //继承了Humans
      }
      var man1=new Man();
      man1.clothing.push("coat");
      alert(man1.clothing);
      var man2=new Man();
      alert(man2.clothing);
      
      //方式二
      function Humans(name){
         this.name=name;
      }
      function Man(){
         Humans.call(this,"mary"); //继承了Humans,同时传递参数
         this.age=38; //实例属性
      }
      var man1=new Man();
      alert(man1.name); //输出mary
      alert(man1.age); //输出38
    借用继承:
      function Humans(name){
         this.name=name;
         this.clothing=["trousers","dress","jacket"];
      }
      Humans.prototype.sayName=function(){
         alert(this.name);
      }
      function Man(name,age){
         Humans.call(this,name); //继承属性
         this.age=age;
      }
      Man.prototype=new Humans();  //继承方法
      Man.prototype.sayAge=function(){
         alert(this.age);
      }
      var man1=new Man("mary",38);
      man1.clothing.push("coat");
      alert(man1.clothing); //输出trousers dress jacket coat
      man1.sayName();  //输出mary
      man1.sayAge(); //输出38
      var man2=new Man("tom",26);
      alert(man2.clothing); //输出trousers dress jacket
      man2.sayName(); //输出tom
      man2.sayAge(); //输出26
     
  • 相关阅读:
    System.ServiceModel.CommunicationException: 接收HTTP 响应时错误发生
    "智囊"王沪宁先后辅佐三任总书记 _中国经济网
    xx
    我告诉你哦,最好吃的海南鸡饭不在海南…
    服务密码重置_中国移动通信
    移动服务密码怎么查_服务密码忘记了怎么办_百度经验
    http://www.sohu.com/a/162795109_465329
    首页--易配菜-中国餐饮行业最大的综合解决方案提供商
    浙江方圆工程咨询有限公司
    MySQL中间件方案盘点_搜狐科技_搜狐网
  • 原文地址:https://www.cnblogs.com/wnwn/p/10997965.html
Copyright © 2011-2022 走看看