享元模式主要是对其数据,方法共享分离,它将数据和方法分成内部数据,内部方法和外部数据。外部方法,内部方法与内部数据指的是相似或者共有的数据和方法
享元动作
var FlyWeight = {
movex: function(x){
this.x = x;
},
mobeY: function(y){
this.y = y;
}
}
var Player = function(x,y,c){
this.x = x;
this.y = y;
this.color = c;
}
Player.prototype = FlyWeight;
Player.prototype.changeC = function(c){
this.color = c;
}
var Sprit = function(x,y,r) = {
this.x= x;
this.y = y;
this.r = r;
}
Sprit.prototype = FlyWeight;
Sprit.prototype.changeR = function(r){
this.r = r;
}
var player = new Plyaer(5,6,'red');
player.moveX(6)
var sporit1 = new Sprit(2,3,4)
sporit1.moveX(6)
网友评论