简体   繁体   English

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

[英]Why does this Pair class not work as I expect

My background is C++ where it seems working with objects is very different to Javascript. 我的背景是C ++,似乎与对象一起使用的对象非常不同。 C++ has a Pair object like the Pair below I am trying to re-create. C ++具有一个Pair对象,例如下面我要重新创建的Pair。 But this class does not work as I intended. 但是,该类无法按我的预期工作。 Perhaps it is totally the wrong approach for Javascript. 对于Javascript,也许这完全是错误的方法。 My ultimate aim is to parse a string of key1=val1;key2=val2;key3=val3; 我的最终目的是解析一个字符串key1 = val1; key2 = val2; key3 = val3; into an associative array. 进入关联数组。 So any tips on that would be helpful. 因此,任何有关此的提示将有所帮助。 But my first problem is the Pair class below. 但是我的第一个问题是下面的Pair类。 any help would be much appreciated. 任何帮助将非常感激。

<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>

You don't need those prototype functions at all, and in getValueAndKey function you have to return new Pair(strkey, strvalue) instead of just Pair. 您根本不需要那些原型函数,在getValueAndKey函数中,您必须返回新的Pair(strkey,strvalue)而不是Pair。 Maybe this solves your problem: 也许这可以解决您的问题:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM