JScript.NET 私有变量
我想知道 JScript.NET 私有变量。请看一下下面的代码:
import System;
import System.Windows.Forms;
import System.Drawing;
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
}
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
}
var addHeader = function(){
out('header');
}
return {
endDocument: function(){
state = 1;
addHeader();
out('endDocument');
},
beginDocument: function(){
beginPage();
}
}
}
var j = new jsPDF();
j.beginDocument();
j.endDocument();
输出:
beginPage 2
header 2
endDocument 2
如果我在任何浏览器中运行相同的脚本,输出是:
beginPage 2
header 1
endDocument 1
为什么会这样?
谢谢, 保罗.
I'm wondering about JScript.NET private variables. Please take a look on the following code:
import System;
import System.Windows.Forms;
import System.Drawing;
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
}
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
}
var addHeader = function(){
out('header');
}
return {
endDocument: function(){
state = 1;
addHeader();
out('endDocument');
},
beginDocument: function(){
beginPage();
}
}
}
var j = new jsPDF();
j.beginDocument();
j.endDocument();
Output:
beginPage 2
header 2
endDocument 2
if I run the same script in any browser, the output is:
beginPage 2
header 1
endDocument 1
Why it is so??
Thanks,
Paul.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是猜测,但 JScript.NET 似乎不支持与 EMCAScript 相同的闭包,因此 endDocument() 中的
state
变量并未引用外部函数的私有成员,而是而是一个局部变量(未声明)。奇怪的。Just a guess, but it appears that JScript.NET doesn't support closures the same way as EMCAScript, so the
state
variable in endDocument() isn't referencing the private member of the outer function, but rather an local variable (undeclared). Odd.在这里调用 jsPDF 时不必使用 new,因为您使用的是单例模式。 jsPDF 返回一个对象文字,因此即使没有 new,您也可以访问 beginPage 和 endDocument 方法。老实说,我不知道在返回对象文字的函数上使用 new 时规范要求什么,因此我不确定 JScript.NET 是否出错或浏览器出错。但现在尝试摆脱 jsPDF() 之前的 new 或将您的函数更改为:
这将允许您使用 new 关键字并创建多个 jsPDF 对象。
You don't have to use new when calling jsPDF here since you're using a singleton pattern. jsPDF is returning an object literal so even without new you'll have access to the beginPage and endDocument methods. To be perfectly honest I don't know what the specifications call for when using new on a function that returns an object literal so I'm not sure if JScript.NET is getting it wrong or the browser. But for now try either getting rid of the new before jsPDF() or change your function to this:
That will allow you to use the new keyword and create more than one jsPDF object.
我也遇到过同样的问题。在下面的代码中,绑定到 fun 的闭包应该只包含一个名为 result 的变量。正如代码所示,带有一个参数的函数中的变量 result 似乎与闭包中的 result 变量不同。
如果在此函数中
删除该行,则该行中的结果
将引用闭包中的结果。
I've come across the same problem. In the following code, the closure bound to fun should contain only one variable called result. As the code stands, the variable result in the function with one parameter seems to be different to the result variable in the closure.
If in this function the line
is removed, then the result in the line
refers to the result in the closure.