美文网首页
对象原型

对象原型

作者: 字母31 | 来源:发表于2017-11-23 21:34 被阅读0次

    1、OOP 指什么?有哪些特性

    a)OPP(Object-Oriented Programming, 面向对象编程),面向对象程序设计中的概念主要包括:对象、类、数据抽象、数据继承、多态、封装、动态绑定、消息传递。其中类和对象是最重要的两个概念,类是对具有某些功能和属性的抽象模型,而对象就是类的实例化。
    b)特性
     1、封装:将一个类的使用与实现分开,只保留部分接口和方法与外界发生联系
     2、继承:子类可以继承父类的属性和方法,达到了代码的可重复利用
     3、多态:子类可以添加或重写属性和方法,所以不同的子类的同一个方法表现可能不同

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

    function Person (name,age){
        this.name = name
        this.age = age
        this.info = function(){
           console.log(this.name,this.age)
       }
    }
    
    var person1 = new Person('xiaogu',3)
    var person2 = new Person('hunger',3)
    person1.info()
    person2.info()
    

    3、prototype 是什么?有什么特性

    a)prototype 是原型,当一个函数被创建时,就会为其添加一个prototype 属性,指向该函数的原型对象,上面的属相和方法可以被子类继承,null除外
    b)当使用构造函数生成一个对象时,该对象会有一个proto属性,指向父类的prototype属性

    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('前端');
    

    原型图

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

    function Car (name,color,satus){
       this.name = name
       this.color = color
       this.status = status
    }
    Car.prototype = {
       run: function(){console.log('running')},
       stop:function(){console.log('stop')},
       getStatus:function(){console.log('status')}
    }
    var car = new Car('BMW','red',100)
    

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

    function GoTop ($ct){
       this.ct = $ct
       this.target = $('<button class="target">GoTop</button>')
       this.createNode()
       this.bindEvent()
    }
    GoTop.prototype = {
       createNode: function(){
           this.ct.append(this.target)
       },
       bindEvent: function(){
           this.target.click(function(){
               $(window).scrollTop(0)
           })
       }
    }
    new GoTop($('.ct')) 
    

    7、使用木桶布局实现一个图片墙

    代码
    预览

    相关文章

      网友评论

          本文标题:对象原型

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