美文网首页
对象_原型

对象_原型

作者: 向前冲冲的蜗牛 | 来源:发表于2017-10-24 00:13 被阅读0次

    问题1: OOP 指什么?有哪些特性

    oop(全称object-oriented programming)是面向对象程序设计,是种具有对象概念的程序变成典范。
    特征有封装性,继承,多态
    封装是将一个类和实现分开,只保留部分接口和方法与外部联系
    继承是子类继承其父类种的属性和方法,并且可以添加新的属性和方法。
    多态:子类继承了父级类种的属性和方法,并对其中部分方法进行重写

    问题2: 如何通过构造函数的方式创建一个拥有属性和方法的对象?

    function dog(name){
      this.dogname=name;
      this.say=function()
      {
        console.log(this.dogname);
      }
    }
    var kok=new dog("kok");
    kok.say();
    

    问题3: prototype 是什么?有什么特性

    prototype是原型,每个函数会自动生成,prototype指向函数的原型对象,原型对象中的方法能够被子类复用和共享,另外原型对象中有一个constructor属性,这个属性指向构造函数。
    当使用构造函数生成一个对象的时候,该对象就会自动生成一个_ _ proto _ _属性,这个属性指向构造函数的原型对象。

    image.png

    问题4:画出如下代码的原型图

    function People (name){
      this.name = name;
      this.sayName = function(){
        console.log('my name is:' + this.name);
      }
    }
    
    People.prototype.walk = function(){
      console.log(this.name + ' is walking');  
    }
    
    var p1 = new People('饥人谷');
    var p2 = new People('前端');
    
    image.png

    问题5: 创建一个 Car 对象,拥有属性name、color、status;拥有方法run,stop,getStatus

    function Cargroup(name,color,status)
    {
      this.name=name;
      this.color=color;
      this.status=status;
    }
    Cargroup.prototype.run=function()
    {
        console.log("runing")
    }
    Cargroup.prototype.stop=function()
    {
      console.log("stop")
    }
    Cargroup.prototype.getstatus=function()
    {
      console.log(this.status);
    }
    var Car=new Cargroup("Mini",'red','stop');
    Car.run();
    

    问题6: 创建一个 GoTop 对象,当 new 一个 GotTop 对象则会在页面上创建一个回到顶部的元素,点击页面滚动到顶部。拥有以下属性和方法

    1. `ct`属性,GoTop 对应的 DOM 元素的容器
    2.  `target`属性, GoTop 对应的 DOM 元素
    3.  `bindEvent` 方法, 用于绑定事件
    4 `createNode` 方法, 用于在容器内创建节点
    

    代码:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
      <style>
      .box{
      height:600px;
      background:green;
    }
    .wrapper{
      border:1px solid red;
      position:fixed;
      bottom:30px;
      background:yellow;
      left:50%;
    }
      
      </style>
    <body>
      <div class="box"></div>
    <div class="wrapper">
      
    </div>
      <script>
      function GotTop(ct)
    {
      this.ct=ct;
      this.target=this.createNode(this.ct);
      this.bindEvent('click')
    }
    GotTop.prototype.createNode=function(par){
     var div=document.createElement('div');
      div.innerText="回到顶部"
      par.appendChild(div);
      return div;
      
    }
    GotTop.prototype.bindEvent=function(event)
    {
      this.target.addEventListener(event,function(){
       scrollTo(0,0);
       
      });
    }
    var s=document.querySelector(".wrapper");
    var f=new GotTop(s);
    
      </script>
    </body>
    </html>
    

    效果
    https://jsbin.com/moredulema/1/edit?html,output

    问题7: 使用木桶布局实现一个图片墙

    代码:https://github.com/HappyXll/WEB-KNOWLEDGE/blob/master/cask-layout/casklayout2.html

    image.png

    相关文章

      网友评论

          本文标题:对象_原型

          本文链接:https://www.haomeiwen.com/subject/uvuouxtx.html