javascript 老王开车去东北

[Decode error - output not utf-8]
魔女
飞
奔驰
去
华南
[Finished in 1.1s]

需要变化的对象进行隔离。正是编程的乐趣之处

/**
 * by JackChen 2016-3-15 9.21.57
 * 基于马士兵老师的设计模式视频
 * 入门: 老王开车去东北 封装
 */
//////////////////////////////////////////////////////////////////////
/// 封装

//////////////////////////////////////////////////////
var Driver = function(name) {
    var self = this;

    self.name = name;
};
Driver.prototype = {};
Driver.prototype.constructor = Driver;

Driver.prototype.Name = function() {
    var self = this;
    console.log(self.name);
};

Driver.prototype.Do = function() {
    var self = this;
    console.log('开');
};

//////////////////////////////////////////////////////
var Witch = function(name) {
    var self = this;

    self.name = name;
};
Witch.prototype = new Driver();
Witch.prototype.constructor = Witch;

Witch.prototype.Name = function() {
    var self = this;
    console.log(self.name);
};

Witch.prototype.Do = function() {
    var self = this;
    console.log('飞');
};







//////////////////////////////////////////////////////
var Car = function(name) {
    var self = this;

    self.name = name;    
};
Car.prototype = {};
Car.prototype.constructor = Car;

//循环调用自己的子元素
Car.prototype.Name = function() {
    var self = this;
    console.log(self.name);
};











//////////////////////////////////////////////////////
var Place = function(name) {
    var self = this;

    self.name = name;    
};
Place.prototype = {};
Place.prototype.constructor = Place;

//循环调用自己的子元素
Place.prototype.Name = function() {
    var self = this;
    console.log(self.name);
};











//////////////////////////////////////////////////////
var Travel = function(driver, tool , place) {
    var self = this;
    self.driver = driver;
    self.tool = tool;
    self.place = place;
};
Travel.prototype = {};
Travel.prototype.constructor = Travel;

//循环调用自己的子元素
Travel.prototype.travel = function() {
    var self = this;
    self.driver.Name();
    self.driver.Do();
    self.tool.Name();
    console.log('去');
    self.place.Name();
};













/////////////////////////////////////////////////////////////////////
/// 测试

// console.log('老王开车去东北');

var driver = new Witch("魔女");
var car = new Car("奔驰");
var place = new Place("华南");

var travel = new Travel(driver, car, place);
travel.travel();
View Code
原文地址:https://www.cnblogs.com/Again/p/5284834.html