用JavaScript构建模型

用JavaScript构建模型,javascript,model,Javascript,Model,有人问我这个问题: 编写一个JavaScript函数模型,该模型执行以下功能: 我提供了以下解决方案,但我想知道我的解决方案是否正确,或者是否有更好的编写方法。请帮忙 我的解决方案: 函数模型参数{ 如果!args{ this.store={}; }否则{ this.store=args; } this.listeners={}; this.listenerKeys=[]; } Model.prototype.set=函数k,v{ //k==年龄 const.oldValue=this.sto

有人问我这个问题:

编写一个JavaScript函数模型,该模型执行以下功能:

我提供了以下解决方案,但我想知道我的解决方案是否正确,或者是否有更好的编写方法。请帮忙

我的解决方案:

函数模型参数{ 如果!args{ this.store={}; }否则{ this.store=args; } this.listeners={}; this.listenerKeys=[]; } Model.prototype.set=函数k,v{ //k==年龄 const.oldValue=this.store[k]?this.store[k]:未定义; //检查我们是否正在听钥匙。 如果此.listenerKeys.indexOfchange>-1{ //如果我们这样做了,那么获取回调列表。 const list=this.listeners[change]; //迭代回调并激发em';- lists.forEachlist=>{ listk,oldValue,v;//callbackattrName,oldValue,newValue }; } this.store[k]=v; } Model.prototype.get=functionk{ 如果这个。商店。有自己的财产{ 返回此。存储[k]; }否则{ 抛出新错误,不存在密钥; } } Model.prototype.has=functionk{ const keys=Object.keysthis.store; ifkeys.indexOfk>-1{ 返回true; }否则{ 返回false; } } Model.prototype.unset=functionk{ 删除此。存储[k]; } Model.prototype.on=函数名,回调{ this.listenerKeys.pushname; 如果this.listeners.hasOwnPropertyname{ this.listeners[name].pushcallback; }否则{ this.listeners[name]=[];//创建一个数组 this.listeners[name].pushcallback;//push } } //问题: const person=新模型; person.设置'name','Bob' console.logperson.get'name'//Bob console.logperson.has'name'//true person.unset'name' console.logperson.has'name'//false const person2=新型号{姓名:'Bob',年龄:27}; 控制台。logperson2。获取“名称” 控制台。logperson2。获取“年龄” person2.关于“更改”,函数名,旧值,新值{ console.log`1.${attrName}已从${oldValue}更改为${newValue}` } //首先 person2.关于“更改”,函数名,旧值,新值{ console.log`2.${attrName}已从${oldValue}更改为${newValue}` } person2.set'age',28//2打印的年龄声明已从27岁更改为28岁 /* //第二部分。让我们稍后再考虑 //仅当名称更改时才会触发此操作 person2.on'change:name',functionoldValue,newValue{ console.log`3.${attrName}已从${oldValue}更改为${newValue}` }
*/ 最优雅的是:

class Model extends Map {
 constructor(obj){
   super();

   this.listeners = {};

   for(const key in obj)
     this.set(key, obj[key]);
 }

 set(key,value){
   for(const listener of (this.listeners["change"] || []))
      listener(key, this.get(key), value);

   for(const listener of (this.listeners["change:" + key] || []))
      listener(key, this.get(key), value);

   super.set(key,value);
 }

 on(evt, func){
   (this.listeners[evt] || (this.listeners[evt] = []).push(func);
 }

 unset(key){ this.delete(key); }
}

谢谢你@Jonas,我很想看到你同样的实现。感谢你花时间做这件事;除了评论和林汀,非常感谢!谢谢你的时间。为什么要扩展地图?我没有得到它,因为它为has/get/set/delete等提供了方法。
class Model extends Map {
 constructor(obj){
   super();

   this.listeners = {};

   for(const key in obj)
     this.set(key, obj[key]);
 }

 set(key,value){
   for(const listener of (this.listeners["change"] || []))
      listener(key, this.get(key), value);

   for(const listener of (this.listeners["change:" + key] || []))
      listener(key, this.get(key), value);

   super.set(key,value);
 }

 on(evt, func){
   (this.listeners[evt] || (this.listeners[evt] = []).push(func);
 }

 unset(key){ this.delete(key); }
}