在子模块模式中对象文字和"this" [英] Objects literal and 'this' in submodule pattern

查看:73
本文介绍了在子模块模式中对象文字和"this"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用子模块模式代码.要创建带有对象文字的子模块,问题是this,因为子模块内的对象是MODULE而不是我的对象文字.有什么主意吗?

Im working in a sub-module pattern code. Want to create sub-modules with objects literals, the problem is this for the objects inside the sub-module is MODULE and not my object literal. Any idea?

var MODULE.sub = (function () {

   var myObject = {

       key: value,

       method: function () {
           this.key // this = MODULE and not MyObject... :(
       }

   };

   return myObject.method;

}(MODULE));

推荐答案

已解决...只需在MODULE.sub中返回一个调用公共方法的函数即可.我不知道最好的方法是什么

Solved... just return a function in MODULE.sub calling the public method. I don't know if is the best approach

var MODULE.sub = (function () {

   var myObject = {

       key: value,

       method: function () {
           this.key // this = myObject :)
       }

   };

   return function () {
        myObject.method();
    }

}(MODULE));

这篇关于在子模块模式中对象文字和"this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆