美文网首页
对象_原型

对象_原型

作者: ahong_吴 | 来源:发表于2017-01-28 19:01 被阅读44次
    问题1: OOP 指什么?有哪些特性
    • OOP指:Object Oriented Programming,面向对象编程,是一种解决代码复用的设计和编程方法。这种方法让相近相似的操作逻辑和操作应用数据、状态,以类的型式描述出来,以对象实例的形式复用,以达到提高开发效率的作用。

    • 特性:

    • 封装性:种把数据和方法绑定在一起使用的方法

    • 继承性:子类自动继承其父级类中的属性和方法

    • 多态性:不同的类可以定义相同的属性或方法

    问题2:如何通过构造函数的方式创建一个拥有属性和方法的对象?
    function People(name,age){
         this.name = name;
         this.age = age;
     }
    People.prototype.sayName = function(){
         console.log(this.name)
     }
     var p1 = new People('hunger','20');
     p1.sayName();
    
    问题3: prototype 是什么?有什么特性
    • 几乎任何对象有一个prototype属性,该属性指向的是这个对象的原型。
    • 这个对象的原型像一个公用的场所,里面的所有属性和方法,创建出来的所有实例都能继承使用。
    问题4:画出如下代码的原型图
    Paste_Image.png 原型对象.png
    问题5: 创建一个 Car 对象,拥有属性name、color、status;拥有方法run,stop,getStatus
    function Car(name,color,status){
          this.name = name;
          this.clor = color;
          this.status = status;
        }
        Car.prototype.run = function(){
          console.log(this.name);
        }
        Car.prototype.stop = function(){
          console.log(this.name);
        }
        Car.prototype.getStatus = function(){
          console.log(this.name);
        }
        var oneCar = new Car('a','red','1');
        var twoCar = new Car('b','blue','2');
        oneCar.run();
        oneCar.stop();
        oneCar.getStatus();
        twoCar.run();
        twoCar.stop();
        twoCar.getStatus();
    
    问题6: 创建一个 GoTop 对象,当 new 一个 GotTop 对象则会在页面上创建一个回到顶部的元素,点击页面滚动到顶部。拥有以下属性和方法

    在线预览

    相关文章

      网友评论

          本文标题:对象_原型

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