饥人谷技术博客

对象_原型

2017-08-27  本文已影响0人  liushaung

OOP 指什么?有哪些特性?

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

function People(name) {
  this.name = name
  this.sayName = function () {
    console.log(this.name)
  }
}
var p1 = new People('bill')
p1.name  // bill
p1.syaName() // bill

prototype 是什么?有什么特性

画出如下代码的原型图

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

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

var Car = {
  name: 'bill',
  color: 'black',
  status: 'OK',
  run: function () {
    console.log('runing...')
  },
  stop: function() {
    console.log('stop')
  },
  getStatus: function() {
    console.log(this.status)
  }
}

创建一个 GoTop 对象,当 new 一个 GotTop 对象则会在页面上创建一个回到顶部的元素,点击页面滚动到顶部。

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

上一篇下一篇

猜你喜欢

热点阅读