Javascript IIFE作为对象的属性(方法) [英] Javascript IIFE as an object's property (method)
问题描述
我正在尝试使用IIFE作为方法(可能是错误的)。
I'm trying to use an IIFE as a method (which might be wrong).
为什么?因为,我正在尝试实现代理设计模式。
Why ? Because, I'm trying to implement the proxy design pattern.
在adobe扩展脚本中,有一个 app对象可以访问文档,例如-
In adobe extendscript, there is an "app" object to access documents, etc, like -
var length = app.activeDocument.length; // or some other property
现在,我想在 app周围放置一个代理。因此,我创建了一个代理对象-
Now, I wanted to put a proxy around "app". So I created a proxy object -
var AppProxy = {
activeDocument: function() { // do stuff...; return app.ActiveDocument; }
}
但是现在,这是我必须访问的方式-
But now, this is how I had to access it -
var length = AppProxy.activeDocument().length;
但这是我要访问的方式-
But this is how I want to access it -
var length = AppProxy.activeDocument.length; // no parenthesis
因此,我了解了IIFE,并最终做到了-
So I read about IIFE, and ended up doing this -
var AppProxy = {
activeDocument: (function() {
// do stuff...;
return app.ActiveDocument; })()
}
和预期的一样, AppProxy 后,即会自动调用> AppProxy.activeDocument 。即,在达到 var length = AppProxy.activeDocument.length 之前。
And as expected, the AppProxy.activeDocument gets called automatically when AppProxy is defined, i.e. even BEFORE it reaches var length = AppProxy.activeDocument.length.
那么当将AppProxy定义为对象文字时,如何防止这种情况发生?
是否可以解决我的要求?
So how do I prevent this from happening when AppProxy is defined as an object literal ? Is there a workaround to my requirement ?
谢谢。
推荐答案
但这是我要访问的方式-
But this is how I want to access it -
var length = AppProxy.activeDocument.length ; //没有括号
为此,您需要定义 activeDocument
作为具有 getter 函数的属性。在具有适当支持的浏览器中可以实现ES5 getter和setter,这是所有现代浏览器(不是IE8和更早版本)。 (在ES5之前,存在一些浏览器支持的从未标准化的语法,但再次不支持IE8或更早版本。)。
To do that, you need to define activeDocument
as a property with a getter function. This is possible in browsers with proper support for ES5's getters and setters, which is all modern browsers (not IE8 and earlier). (Before ES5, there was a never-standardized syntax for this that some browsers supported, but again not IE8 or earlier).
在ES5中,您可以使用 Object.defineProperty
或通过在对象初始化程序中定义一个吸气剂。这是 Object.defineProperty
:
In ES5, you can do this either with Object.defineProperty
or by defining a getter in an object initializer. Here's Object.defineProperty
:
// ES5+ only
var AppProxy = {};
Object.defineProperty(AppProxy, "activeDocument", {
get: function() {
// do stuff...;
return app.ActiveDocument;
}
});
此处将其作为对象初始化程序的一部分:
Here's doing it as part of the object initializer:
// ES5+ only
var AppProxy = {
get activeDocument() {
// do stuff...;
return app.ActiveDocument;
}
};
已经做过任何一个,那么:
Having done either of those, then this:
var length = AppProxy.activeDocument.length;
...运行该功能,即使它看起来不一样。函数调用仍然发生,只是被隐藏。
...runs that function, even though it doesn't look like it does. The function call still happens, it's just hidden.
但是如果您需要支持过时的浏览器(即使在2016年中期,IE8仍然占有约5%的市场份额)或如果您不想隐藏正在调用函数的事实,只需调用函数即可:
But if you need to support obsolete browsers (even here in mid 2016, IE8 still has ~5% market share) or if you don't want to hide the fact you're calling a function, just call the function:
var length = AppProxy.activeDocument().length;
这篇关于Javascript IIFE作为对象的属性(方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!