Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2450|回复: 0
打印 上一主题 下一主题

defineLazyGetter

[复制链接]

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
跳转到指定楼层
楼主
发表于 2016-4-21 16:15:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


  const { defineLazyGetter } = require('./LazyGetter.js')

  exports.defineLazyGetter = (aObject, aName, aLambda) => {
  if (Object.prototype.hasOwnProperty.call(aObject, aName)) {
    /* TODO: using global variable to make sure not to be called multiple times */
    console.log(`Define ${aName} on an object multiple times ${components.stack.caller.caller.filename}`)
    return
  }
  return Object.defineProperty(aObject, aName, {
    get: function () {
      // Redefine this accessor property as a data property.
      // Delete it first, to rule out "too much recursion" in case aObject is
      // a proxy whose defineProperty handler might unwittingly trigger this
      // getter again.
      delete aObject[aName];
      let value = aLambda.apply(aObject);
      Object.defineProperty(aObject, aName, {
        value,
        writable: true,
        configurable: true,
        enumerable: true
      });
      return value;
    },
    configurable: true,
    enumerable: true
  });
}




  Globals.app.defineLazyGetter = (name, lambda) => {
    return defineLazyGetter(Globals.app, name, lambda)
  }
  // 调用
  Globals.app.defineLazyGetter('messageCache', () => new Map())

  // 使用
   Globals.app.messageCache.has(msgURI)
   Globals.app.messageCache.get(msgURI)
   Globals.app.messageCache.set(msgURI, result)




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-11-26 13:30 , Processed in 0.077213 second(s), 28 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表