- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在尝试实现一些常见的 JS 概念在小项目中更好地了解如何使用它们。
我一直在做一个简单的游戏,试图理解和使用模块模式和闭包。我正在使用 Stoyan Stefanov 的“模式”中的模块模式书。
我正在努力理解如何最好地混合模块和关闭。
我想知道我是否将以下代码组织在明智的方式?如果是这样,我的问题是:最好的方法是什么修改代码,以便在 $(function(){})
我有访问 update()
函数?
MYAPP.utilities = (function() {
return {
fn1: function(lives) {
//do stuff
}
}
})();
MYAPP.game = (function() {
//dependencies
utils = MYAPP.utilities
return {
startGame: function() {
//initialisation code
//game state, stored in closure
var lives = 3;
var victoryPoints = 0;
function update(){
utils.fn1(lives);
//do other stuff
}
}
}
})();
$(function(){
MYAPP.game.startGame();
//Want to do this, but it won't work
//because I don't have access to update
$('#button').on('click',MYAPP.game.update)
});
我想出了几个可行的选项,但是我想知道他们是否是好的做法,什么是最好的选项是。
选项:
(1) 绑定(bind) $('#button').on('click', ...)
作为startGame 初始化代码。
(2) 将update()
函数赋给一个变量,然后从 startGame 函数返回这个变量,所以在$(function(){})
我们可以updatefn = MYAPP.game.startGame();
然后$('#button').on('click',MYAPP.game.update)
(3)?有没有更好的办法?
非常感谢您的帮助,
罗宾
最佳答案
首先,要以这种方式访问 update
函数,它必须在返回的对象中公开。
return {
update: function() {
[...]
},
startGame: function() {
[...]
this.update();
}
}
调用 obj.method()
会自动将此 method
调用中的 this
引用设置为 obj
。也就是说,调用 MYAPP.game.startGame()
会将 this
设置为 MYAPP.game
在此 startGame
方法调用中.有关此行为的更多详细信息 here .
您还需要将 lives
变量移动到一个公共(public)范围,该范围可通过 startGame
和 update
方法访问,这正是关闭的目的是什么:
MYAPP.game = (function() {
[...]
var lives; //private/privileged var inside the closure, only accessible by
//the returned object's function properties
return {
update: function() {
utils.fn1(lives);
},
startGame: function() {
[...]
lives = 3; //sets the closure scope's lives variable
[...]
this.update();
}
}
})();
在这种情况下,当您想要更改lives
变量时,您需要一些方法来设置它。另一种方法是通过使 lives
变量成为返回对象的属性并通过方法内部的 this.lives
访问它来公开变量。
注意:如果您只是传递对存储为返回对象属性的函数对象的引用,如下所示:
$('#button').on('click', MYAPP.game.update);
点击处理程序中的this
引用将不指向MYAPP.game
,因为传递的函数引用将被直接调用来自 jQuery 核心而不是对象的成员函数调用 - 在这种情况下,this
将指向 #button
元素,因为 jQuery 事件处理程序设置 this
对触发处理程序的元素的引用,如您所见 here .
要补救,您可以使用 Function.bind()
:
$('#button').on('click', MYAPP.game.update.bind(MYAPP.game));
或者旧的函数包装技巧:
$('#button').on('click', function() {
MYAPP.game.update(); //called as method of an obj, sets `this` to MYAPP.game
});
当在 update
方法中使用 this
关键字时,这一点很重要。
关于javascript - 将模块模式与闭包相结合的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16260596/
我发现 hpc 真的很令人困惑,即使在阅读了一些解释并玩了很多之后。 我有一个图书馆 HML , 和两个测试套件 fileio-test和 types-test , 使用 HTF (我打算搬到 tas
创建可设计的 .NET 组件时,您需要提供默认构造函数。来自 IComponent文档: To be a component, a class must implement the IComponen
我想从 dnsmasq 收集和处理日志,我决定使用 ELK。 Dnsmasq 用作 DHCP 服务器和 DNS 解析器,因此它为这两种服务创建日志条目。 我的目标是向 Elasticsearch 发送
我定义了一个名为“folderwatcher”的类(可调用),它正在监视特定文件夹的任何更改。主要代码处于无限循环中,类似于: Monitor a Directory for Changes usin
我正在尝试重现 this plot : 它看起来像库格子中的 xyplot,但我找不到将马赛克图与 xyplot 组合的方法。 有人知道怎么做吗? 最佳答案 你只是在寻找内置于 vcd:::cotab
我遇到了提供字符串列表的情况。列表中的第一个条目是方法的名称。列表中的其余字符串是方法参数。我想使用任务来运行该方法(出于教育目的)。我在找出允许我将方法名称输入任务指令的正确过程时遇到问题。 对于这
为了处理非常大的集合(对于非常大,我的意思是“可能会抛出 OutOfMemory 异常”),使用 Hibernate 似乎有问题,因为通常集合检索是在一个 block 中完成的,即 List valu
我得到了一个包含超过 5000 万条记录的数据库表我需要尽快进行全文搜索。 在一个较小的表上,我只是在文本列上有一个索引,我使用相似性函数来获得相似的结果。我还能够根据 similarity() 的结
我有两个表details 和data 表。我已经加入了两个表并且交叉表功能已经完成。 我只想显示每个 serial 的最新数据。请参阅下面的当前和所需输出。 问题:如何在此交叉表查询中使用 DISTI
我在 Postgre (9.1.9) 中将串联与排序结合起来时遇到了麻烦。比方说,我有一个包含 3 个字段的表格边框: Table "borders" Column
我有一个组件,它使用辅助函数来获取日期列表,然后映射它们。在检索到的数据中,并不总是存在给定阶段的日期,因此我添加了逻辑,以便在该特定日期未定义时返回空字符串。 辅助函数获取属性 Phase =“阶段
我想尝试构建一段干净、漂亮的代码,我可以在其中实现您在下图中看到的结果。在 Firefox、Chrome 或 Safari 中可以,但在 IE 中不行。 我创建了一个 JSFiddle用代码。 基本上
我有一个导航菜单,其中的元素旋转 90 度。我想将其与悬停在导航项上时显示的下拉 block 结合起来,以保持项的动态行为。 动态导航面板的关键CSS代码在这里: .buttons-wrapper {
在 CSS 中,我可以像这样进行选择: input[type="number"], input[type="password"], input[type="text"], textarea, .but
假设我们已经为不同的平台(iOS/Android/Winfon( future ))实现了移动应用程序。所有应用程序都有一些共同的业务逻辑。 例如,计算器应用程序:用户输入两位数,我们的应用程序应该能
在我的 onCreate() 中,我设置了一个进度条,如下所示: getWindow().requestFeature(Window.FEATURE_PROGRESS); getWindow().se
我有一个遗留的 ASP.NET webforms 应用程序,用户通过在服务器端处理的表单登录。如果输入的用户名 + 密码与数据库中的凭据匹配,我会在 session 中设置一些值(例如,当前用户 ID
如何在 TastyPie 中组合多个资源?我有 3 个模型要合并:用户、个人资料和帖子。 理想情况下,我希望配置文件嵌套在用户中。我想从 UserPostResource 公开用户和所有个人资料位置。
假设使用assert() 检查对象函数的先决条件。那么,我该如何编写有意义的单元测试,以便在我将无效参数传递给函数时捕获前提条件失败? 我的意思是,assert() 将 abort(),那么在那之后我
我正在为开源客户端/服务器 4X 策略游戏 Thousand Parsec 构建 Qt 客户端.这是一个 Google Summer of Code 项目。然而,我陷入了死胡同。基本上,客户端通过促进
我是一名优秀的程序员,十分优秀!