为什么这个Pair类不能像我期望的那样工作

时间:2013-03-02 18:29:35

标签: javascript

我的背景是C ++,它似乎与对象一起使用与Javascript非常不同。 C ++有一个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>

1 个答案:

答案 0 :(得分:1)

根本不需要那些原型函数,而在getValueAndKey函数中,你必须返回新的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>