how do I define a function inside an object definition where I create an object from a string by extracting information from the string?
The sting would look like this:
var theString = "some textsome more texteven more text";
I then create an object
someObject = new Reportlet(theString);
from this code:
* function Reportlet(myString){
var extractData (separator) = function(){
var dummy;
var a;
var txt;
if(myString.indexOf(separator) > -1){
dummy = myString.split(separator);
dummy = dummy[1];
a = dummy.indexOf("<");
if(a > 0){
txt = dummy.substr(0,a);
}
else{
txt = dummy;
}
}
else{
txt = "";
}
return(txt);
}
this.abbrevText = extractData("<A>");
this.optText = extractData("<S>");
this.fullText = extractData("<L>");
this.impression = extractData("<I>");
this.keywords = extractData("<X>");
} *
The problem is, how do I handover an argument "separator" to the private function "extractData" within an object?
Thanks Holx
You need to do this:
function Reportlet(myString){
var extractData = function(separator) { // just do this
var dummy;
var a;
var txt;
if(myString.indexOf(separator) > -1){
dummy = myString.split(separator);
dummy = dummy[1];
a = dummy.indexOf("<");
if(a > 0){
txt = dummy.substr(0,a);
}
else{
txt = dummy;
}
}
else{
txt = "";
}
return(txt);
}
this.abbrevText = extractData("<A>");
this.optText = extractData("<S>");
this.fullText = extractData("<L>");
this.impression = extractData("<I>");
this.keywords = extractData("<X>");
}
But remember that the "this" reference inside "extractData" is different from "Reportlet", in other words inside "extractData" the "this" reference is NOT the "Reportlet" instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.