原型模式(creational 5)

189 阅读1分钟

1.5 原型模式(prototype)

  • 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
function Sheep(name, weight) {
  this.name = name;
  this.weight = weight;
}

Sheep.prototype.clone = function() {
  return new Sheep(this.name, this.weight);
};

module.exports = Sheep;
const expect = require('chai').expect
const Sheep = require('../src/creational/prototype/prototype');

describe('原型模式 测试', () => {
	it('sheep', () => {
		var sheep = new Sheep('dolly', 10.3)
		var dolly = sheep.clone()
		expect(dolly.name).to.equal('dolly')
	})
})

es6 实现

class Sheep {

  constructor(name, weight) {
    this.name = name;
    this.weight = weight;
  }

  clone() {
    return new Sheep(this.name, this.weight);
  }
}

export default Sheep;

const expect = require('chai').expect
import Sheep from '../src/creational/prototype/prototype_es6'

describe('原型模式 es6测试', () => {
	it('sheep', () => {
		var sheep = new Sheep('dolly', 10.3)
		var dolly = sheep.clone()

		expect(dolly.name).to.equal('dolly')
		expect(dolly.weight).to.equal(10.3)
		expect(dolly).to.be.instanceOf(Sheep)
		expect(dolly === sheep).to.be.false
	})
})