对象_原型

2017-01-28  本文已影响44人  ahong_吴
问题1: OOP 指什么?有哪些特性
问题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 是什么?有什么特性
问题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 对象则会在页面上创建一个回到顶部的元素,点击页面滚动到顶部。拥有以下属性和方法

在线预览

上一篇 下一篇

猜你喜欢

热点阅读