如何在对象定义中定义函数,通过从字符串中提取信息来从字符串创建对象?
刺痛看起来像这样:
var theString =“一些文本多一些文本甚至更多文本”;
然后我创建一个对象
someObject = new Reportlet(theString);
来自此代码:
* 函数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>");
} *
问题是,如何将参数“分隔符”移交给对象中的私有函数“ extractData”?
谢谢 霍尔克斯
答案 0 :(得分:0)
您需要执行以下操作:
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>");
}
但是请记住,“ extractData”中的“ this”引用与“ Reportlet”不同,换句话说,在“ extractData”中,“ this”引用不是“ Reportlet”实例。