美文网首页
高级1-对象、原型链

高级1-对象、原型链

作者: Maggie_77 | 来源:发表于2017-01-17 21:39 被阅读0次

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

  • OOP:
    Object-oriented programming的缩写,即面向对象程序设计,其中两个最重要的概念就是类和对象。类只是具备了某些功能和属性的抽象模型,而实际应用中需要一个一个实体,也就是需要对类进行实例化,类在实例化之后就是对象。
  • 特性:
    (1)继承性:子类自动继承其父级类中的属性和方法,并可以添加新的属性和方法或者对部分属性和方法进行重写。继承增加了代码的可重用性。
    (2)多态性:子类继承了来自父级类中的属性和方法,并对其中部分方法进行重写。
    (3)封装性:将一个类的使用和实现分开,只保留部分接口和方法与外部联系。

问题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();//hunger

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

prototype:每一个构造函数都有一个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,status){
  this.name = name;
  this.color = color;
  this.status = status;
}
Car.prototype.run = function(){
  console.log(this.name + ' is running');
}
Car.prototype.stop = function(){
  console.log(this.name + ' is stopped');
}
Car.prototype.getStatus = function(){
  console.log(this.name +' is '+ this.status);
}
var myCar = new Car('Maserati','red','running');
var yourCar = new Car('Ferrari','red','stopped')
myCar.run();
myCar.stop();
myCar.getStatus();
yourCar.run();
yourCar.stop();
yourCar.getStatus();

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

1. `ct`属性,GoTop 对应的 DOM 元素的容器
2.  `target`属性, GoTop 对应的 DOM 元素
3.  `bindEvent` 方法, 用于绑定事件
4 `createNode` 方法, 用于在容器内创建节点
function GoTop(ct){
    this.ct = $(ct);
    this.creatNode = function(){
        var target = $('<span class="item" style="border: 1px solid red">回到顶部</span>');
        this.ct.append(target);
        return target;
    };
    this.target=this.creatNode();
    this.bindEvent = function(){
  var _this = this;
        this.target.click(function(){
            $(window).scrollTop(0);
        })
    };
}

var gotop = new GoTop( '.ct' );  
gotop.bindEvent();

相关文章

  • 高级1-对象、原型链

    问题1: OOP 指什么?有哪些特性 OOP:Object-oriented programming的缩写,即面向...

  • Javascript面向对象之组件

    高级面向对象 包装对象(13) js基于原型的程序 String Number Boolean 原型链(14) 实...

  • 高级1-对象、原型

    1. OOP 指什么?有哪些特性? OOP指(Object Oriented Programming,面向对象程序...

  • 高级1-对象_原型

    问题1: OOP 指什么?有哪些特性答:OOP:Object-oriented programming的缩写,即面...

  • 廖雪峰JS小记

    (function(){})() 原型,原型链 浅谈Js原型的理解JS 原型与原型链终极详解 对象 对象:一种无序...

  • lesson 5 面向对象及原型链 2021-04-29

    课程标题 面向对象及原型链 课程目标 面向对象思想 原型及原型链 继承 知识点 面向对象思想 原型链的指向 new...

  • js原型链

    目录 1.对象的原型和原型链1.1什么是原型1.2查看原型1.3对象的原型链 2.使用构造函数2.1 函数的原型链...

  • js原型和原型链

    什么是原型 .什么是原型链? 原型:对象上的内置属性[[prototype]] 原型链:在对象上访问某个属性,如果...

  • js_继承及原型链等(四)

    js_继承及原型链等(三) 1. 继承 依赖于原型链来完成的继承 发生在对象与对象之间 原型链,如下: ==原型链...

  • 原型、原型链和原型继承

    原型链 原型链是一种关系,实例对象和原型对象之间的关系,关系是通过原型(proto)来联系的;实例对象中有prot...

网友评论

      本文标题:高级1-对象、原型链

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