JScript.NET 私​​有变量

发布于 2024-08-19 03:46:49 字数 810 浏览 5 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯叶蝶 2024-08-26 03:46:49

只是猜测,但 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.

满意归宿 2024-08-26 03:46:49

在这里调用 jsPDF 时不必使用 new,因为您使用的是单例模式。 jsPDF 返回一个对象文字,因此即使没有 new,您也可以访问 beginPage 和 endDocument 方法。老实说,我不知道在返回对象文字的函数上使用 new 时规范要求什么,因此我不确定 JScript.NET 是否出错或浏览器出错。但现在尝试摆脱 jsPDF() 之前的 new 或将您的函数更改为:

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');
 };

 this.endDocument = function(){
  state = 1;
  addHeader();
  out('endDocument');
 };

 this.beginDocument: function(){
  beginPage();
 };
}

这将允许您使用 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:

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');
 };

 this.endDocument = function(){
  state = 1;
  addHeader();
  out('endDocument');
 };

 this.beginDocument: function(){
  beginPage();
 };
}

That will allow you to use the new keyword and create more than one jsPDF object.

心凉 2024-08-26 03:46:49

我也遇到过同样的问题。在下面的代码中,绑定到 fun 的闭包应该只包含一个名为 result 的变量。正如代码所示,带有一个参数的函数中的变量 result 似乎与闭包中的 result 变量不同。

如果在此函数中

   result = [];

删除该行,则该行中的结果

  return result;

将引用闭包中的结果。

  var fun = function() {
    var result = [];
    // recursive descent, collects property names of obj
    // dummy parameter does nothing
    var funAux = function(obj, pathToObj, dummy) { 
      if (typeof obj === "object") {
        for (var propName in obj) {
          if (obj.hasOwnProperty(propName)) {
            funAux(obj[propName], pathToObj.concat(propName), dummy); 
          }
        }
      }
      else {
        // at leaf property, save path to leaf
        result.push(pathToObj);
      }
    }
    return function(obj) {
      // remove line below and `result' 3 lines below is `result' in closure
      result = []; // does not appear to be bound to `result' above
      funAux(obj, [], "dummy"); 
      return result; // if result 2 lines above is set, result is closure is a different variable
    };
  }();

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

   result = [];

is removed, then the result in the line

  return result;

refers to the result in the closure.

  var fun = function() {
    var result = [];
    // recursive descent, collects property names of obj
    // dummy parameter does nothing
    var funAux = function(obj, pathToObj, dummy) { 
      if (typeof obj === "object") {
        for (var propName in obj) {
          if (obj.hasOwnProperty(propName)) {
            funAux(obj[propName], pathToObj.concat(propName), dummy); 
          }
        }
      }
      else {
        // at leaf property, save path to leaf
        result.push(pathToObj);
      }
    }
    return function(obj) {
      // remove line below and `result' 3 lines below is `result' in closure
      result = []; // does not appear to be bound to `result' above
      funAux(obj, [], "dummy"); 
      return result; // if result 2 lines above is set, result is closure is a different variable
    };
  }();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文