0

我的背景是 C++,在其中处理对象似乎与 Javascript 非常不同。C++ 有一个 Pair 对象,例如我正在尝试重新创建的下面的 Pair。但是这个类并没有按我的预期工作。也许这对 Javascript 来说是完全错误的方法。我的最终目标是解析一串 key1=val1;key2=val2;key3=val3; 成一个关联数组。因此,任何有关这方面的提示都会有所帮助。但我的第一个问题是下面的 Pair 类。任何帮助将非常感激。

<html>
<head>
<title>my title</title>
<script type="text/javascript">
//Pair class - doesnt seem to work
function Pair(key, value) {
    this.key = key;
    this.value = value;
}

Pair.prototype.Key = function() { return this.key; }
Pair.prototype.Value = function() { return this.value; }


function getValueAndKey(text, splitter) {
   //Pair pair = {};
   if(text) {
     var delim = typeof splitter !== 'undefined' ? splitter : '='; 
     var delimpos = text.indexOf(delim);
     if(delimpos != -1) {
          var strkey = text.substr(0, delimpos);
          var strvalue = text.substr(delimpos+1);
          return Pair(strkey, strvalue);      
     }      
   }
   return null;
}

function doIt() {

   function Options(sourceString) {
      this.source = sourceString;

      //populate key/value pairs from source string
      var vars_array = sourceString.split(";");
      for(var i = 0; i < vars_array.length; ++i) {
         //alert("adding vars_array[" + i + "] = " + vars_array[i]);
         var pair = getValueAndKey(vars_array[i]);
         if(pair)  //pair is ALWAYS undefined :(
            alert("key=" + pair.Key() + " value=" + pair.Value());       
      }
   }

   //exercise class
   var sourceString = "cat=Cookie;legs=4;favouritefood=lamb;type=Siamese;";
   var opts = new Options(sourceString);
}
</script>
</head>

<body onload="doIt();">
some test program
</body>
</html>
4

1 回答 1

1

你根本不需要那些原型函数,并且在 getValueAndKey 函数中你必须返回 new Pair(strkey, strvalue) 而不仅仅是 Pair。也许这可以解决您的问题:

<html>
<head>
<title>my title</title>
<script type="text/javascript">
//Pair class - doesnt seem to work
function Pair(key, value) {
    this.key = key;
    this.value = value;
}

function getValueAndKey(text, splitter) {
   //Pair pair = {};
   if(text) {
     var delim = typeof splitter !== 'undefined' ? splitter : '='; 
     var delimpos = text.indexOf(delim);
     if(delimpos != -1) {
          var strkey = text.substr(0, delimpos);
          var strvalue = text.substr(delimpos+1);
          return new Pair(strkey, strvalue);      
     }      
   }
   return null;
}

function doIt() {

   function Options(sourceString) {
      this.source = sourceString;

      //populate key/value pairs from source string
      var vars_array = sourceString.split(";");
      for(var i = 0; i < vars_array.length; ++i) {
         //alert("adding vars_array[" + i + "] = " + vars_array[i]);
         var pair = getValueAndKey(vars_array[i]);
         if(pair)  //pair is ALWAYS undefined :(
            alert("key=" + pair.key + " value=" + pair.value);       
      }
   }

   //exercise class
   var sourceString = "cat=Cookie;legs=4;favouritefood=lamb;type=Siamese;";
   var opts = new Options(sourceString);
}
</script>
</head>

<body onload="doIt();">
some test program
</body>
</html>
于 2013-03-02T18:34:54.737 回答