[英]One Way Communications Between Model and Support
In the fiddle below, I'd like Mo
to be able to call Su
but not the other way around. 在下面的小提琴中,我希望
Mo
能够给Su
打电话,但不能打电话给Su
。
How can I implement this. 我该如何实施。 I don't want to use events.
我不想使用事件。 Just native functionality of JavaScript.
只是JavaScript的本机功能。
Models are the traditional models found in the MVC. 模型是MVC中的传统模型。 Support contains support functions needed by the model to run.
支持包含模型运行所需的支持功能。
Mo -> Su 莫->苏
If you don't want "anything else accessing Mo", you can make Mo private. 如果您不希望“其他任何人可以访问Mo”,则可以将Mo设为私有。 It is done by making function
Model
into constructor. 这是通过将函数
Model
构造为构造函数来完成的。
$Frame.Model = function(model_object){
var privateFunc = function() {
//Private function of Model objects. Can access Su.
}
this.publicFunction = function() {
//Public function of Model objects. Anyone can access this by Mo.publicFunction(). Also can access Su.
}
}
This function is a CONSTRUCTOR. 此函数是一个构造函数。 So to make Mo, do
所以要让莫做
Mo = new $Frame.Model();
Remember not to forget the new. 记住不要忘记新事物。 Or else things get messed up.
否则事情就搞砸了。 To prevent the "mess" (which happens because the this inside the constructor refers to
window
if you forget the new), do this: 为了防止“混乱”(发生这种情况是因为如果您忘记了新的,则构造函数中的this会引用
window
),请执行以下操作:
$Frame.Model = function(model_object){
if (!(this instanceof $Frame.Model))
return new $Frame.Model();
var privateFunc = function() {
//Private function of Model objects. Can access Su.
}
this.publicFunction = function() {
//Public function of Model objects. Anyone can access this by Mo.publicFunction()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.