1 Star 0 Fork 0

唐心/JSPatternDesign

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
strategy.js 3.13 KB
一键复制 编辑 原始数据 按行查看 历史
唐心 提交于 2021-10-05 00:34 . init
//正式,注意作用域和浅复制,new的区别
// (function () {
var maintest3=1;
let maintest2 =2;
var Class={
create: function(){
return function(){
this.initialize.apply(this,arguments);//改变函数执行的上下文。区别:bind 是返回改变后的函数,不会立即执行。 apply call 可以传数组。
}
}
}
var DicApplyAdd_Scrap = Class.create();//这里仅仅是返回了一个函数 。//= Innolux.Page.DicApplySelect_Scrap;;
//区别 The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
// Parameters
// proto https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
// The object which should be the prototype of the newly-created object.
// 技术分析
// propertiesObject Optional
// If specified and not undefined, an object whose enumerable own properties (that is, those properties defined upon itself and not enumerable properties along its prototype chain) specify property descriptors to be added to the newly-created object, with the corresponding property names. These properties correspond to the second argument of Object.defineProperties().
// 抽象基类
DicApplyAdd_Scrap.prototype={//增加属性与方法。
initialize:function(){
//this.name = name;
// this.doSomething
},
getName:function(){
// return this.name;
return "base class?"
},
doSomething:function(){
console.log("base class do somethin fn");
},
// curr_strategy:function default_strategy() {
// console.log("default_strategy");
// }
}
//context上下文
// Context -- In example code: Shipping
// maintains a reference to the current Strategy object
// supports interface to allow clients to request Strategy calculations
// allows clients to change Strategy
let Context = function () {
this.strategy = null;
}
Context.prototype = {//?
setStrategy:function (strategy) {
this.strategy=strategy;
},
exeStrategy:function (params) {
this.strategy.doSomething();
}
}
// 改进:先new一个基类。但这样不符合oop,不应该new 基类的。
var client = new DicApplyAdd_Scrap();
// strategys
let vendor_s = function ( ) {
return Object.assign({...client},{ //...// 空的。。。
doSomething: function () {
console.log("具体策略vendor做something");
}// 覆盖掉 DicApplyAdd_Scrap基类的dosomething方法
});//返回修改后的目标对象
}
let GW_s = function ( ) {
return Object.assign({...client},{
doSomething: function () {
console.log("具体策略GW做something");
}
});
}
const status_is_vendor = new vendor_s();
const status_is_GW = new GW_s();
status_is_vendor.doSomething();
status_is_GW.doSomething();
status_is_GW.prototype.doSomething();//还是默认的没有被改变。字面量?
const status_switch =new Context();//必须new 下面才有setStrategy这个方法。
status_switch.setStrategy(status_is_vendor);
status_switch.exeStrategy();
console.log(client.getName());
// });
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dangxin/jspattern-design.git
git@gitee.com:dangxin/jspattern-design.git
dangxin
jspattern-design
JSPatternDesign
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385