【问题标题】:JScript.NET private variablesJScript.NET 私​​有变量
【发布时间】:2011-01-08 15:24:41
【问题描述】:

我想知道 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

为什么会这样??

谢谢, 保罗。

【问题讨论】:

    标签: variables closures private jscript.net


    【解决方案1】:

    只是一个猜测,但 JScript.NET 似乎不像 EMCAScript 那样支持闭包,所以 endDocument() 中的 state 变量不是引用外部函数的私有成员,而是局部变量(未声明)。奇怪。

    【讨论】:

    【解决方案2】:

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

    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 对象。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。在下面的代码中,绑定到 fun 的闭包应该只包含一个名为 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
          };
        }();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-25
        • 2013-06-22
        • 1970-01-01
        • 2012-10-26
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多