# 第 1 章：我们在做什么？

## 一个简单例子

``````var Flock = function(n) {
this.seagulls = n;
};

Flock.prototype.conjoin = function(other) {
this.seagulls += other.seagulls;
return this;
};

Flock.prototype.breed = function(other) {
this.seagulls = this.seagulls * other.seagulls;
return this;
};

var flock_a = new Flock(4);
var flock_b = new Flock(2);
var flock_c = new Flock(0);

var result = flock_a.conjoin(flock_c).breed(flock_b).conjoin(flock_a.breed(flock_b)).seagulls;
//=> 32
``````

``````var conjoin = function(flock_x, flock_y) { return flock_x + flock_y };
var breed = function(flock_x, flock_y) { return flock_x * flock_y };

var flock_a = 4;
var flock_b = 2;
var flock_c = 0;

var result = conjoin(breed(flock_b, conjoin(flock_a, flock_c)), breed(flock_a, flock_b));
//=>16
``````

``````var add = function(x, y) { return x + y };
var multiply = function(x, y) { return x * y };

var flock_a = 4;
var flock_b = 2;
var flock_c = 0;

//=>16
``````

``````// 结合律（assosiative）

// 交换律（commutative）

// 同一律（identity）

// 分配律（distributive）
``````

``````// 原有代码

// 再应用分配律
``````