面向对象1

2017-09-26  本文已影响0人  zh_yang

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

面向对象编程 (英文: Object-Oriented Programming, 缩写:OOP ) 是一种程序设计思想。它是指将数据(data) 封装( encapsulated )进对象( objects)中 。然后操作对象, 而不是数据自身。——MDN—JavaScript面向对象简介

它主要有三个特性:封装、继承与多态

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

function Person(name){
  this.name = name
  this.say = function (){
    console.log('My name is ' + this.name)
  }
}
var xiaoming = new Person('Wangxiaoming')
console.log(xiaoming.name)
//"Wangxiaoming"
xiaoming.say()
//"My name is Wangxiaoming"

//或:

function Student(name){
  this.name = name
}
Student.prototype = {
  say:function (){
    console.log(this.name + ' is a student.')
  }
}
var xiaohong = new Student('Xiaohong')
console.log(xiaohong.name)
//"Xiaohong"
xiaohong.say()
//"Xiaohong is a student."

3、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('前端');
image.png

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(){
    this.status = 30
  },stop: function(){
    this.status = 0
  },getStatus: function(){
  console.log(this.status)
  }
}

var car = new Car('BMW','red',0)

car.getStatus()
//0
car.run()
car.getStatus()
//30
car.stop()
car.getStatus()
//0

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

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

预览链接:http://js.jirengu.com/pefag

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

代码地址:https://github.com/jirengu-yang/resume/tree/master/task3-1
预览链接:https://jirengu-yang.github.io/resume/task3-1/cask-layoutIII

926-1.gif
上一篇下一篇

猜你喜欢

热点阅读