2010-03-06 2 views
1

경고하지 않습니다 I line.charAt (0)을 실행하십시오. charAt는 함수가 아닙니다.
경고 (선) 경고 '교류'및 대해서 typeof (라인) '문자열'이지만 수 charAt는 (라인) 경고 '교류' 대해서 typeof (라인) '문자열'</p> <p>있는 기능

어떻게 수 charAt이 UTF-8로 작동하도록 ..., 작동,

나는 그것이 내가 열 파일의 UTF-8 인코딩 생각 'http://www.google.com/'라인을 때?

업데이트 :

authority = 'orkut.com.br'; 
lines = sc_geteffectivetldnames(); 
lines = sc_preparetouse(lines); 
domainname = sc_extractdomainname(authority, lines); 

코드는 :

http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1 코드를 실행하려면 effective_tld_names.dat

나의 확장의 크롬 폴더에

function sc_geteffectivetldnames() { 
    var MY_ID = "[email protected]"; 
    var em = Components.classes["@mozilla.org/extensions/manager;1"]. 
      getService(Components.interfaces.nsIExtensionManager); 
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "chrome/effective_tld_names.dat"); 
    var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]. 
          createInstance(Components.interfaces.nsIFileInputStream); 
    istream.init(file, 0x01, 0444, 0); 
    istream.QueryInterface(Components.interfaces.nsILineInputStream); 
    var line = {}, lines = [], hasmore; 
    do { 
     hasmore = istream.readLine(line); 
     lines.push(line.value); 
    } while(hasmore); 
    istream.close(); 
    return lines; 
} 

function sc_preparetouse(lines) { 
    lines = sc_notcomment(lines); 
    lines = sc_notempty(lines); 
    return lines; 
} 
function sc_notcomment(lines) { 
    var line; 
    var commentre; 
    var matchedcomment; 
    var replacedlines; 
    replacedlines = new Array(); 
    var i = 0; 
    while (i < lines.length) { 
     line = lines[i]; 
     commentre = new RegExp("^//", 'i'); 
     matchedcomment = line.match(commentre); 
     if(matchedcomment) { 
      lines.splice(i, 1); 
     } else { 
      i++; 
     } 
    } 
    return lines; 
} 
function sc_notempty(lines) { 
    var line; 
    var emptyre; 
    var matchedempty; 
    var replacedlines; 
    replacedlines = new Array(); 
    var i = 0; 
    while (i < lines.length) { 
     line = lines[i]; 
     emptyre = new RegExp("^$", 'i'); 
     matchedempty = line.match(emptyre); 
     if(matchedempty) { 
      lines.splice(i, 1); 
     } else { 
      i++; 
     } 
    } 
    return lines; 
} 
function sc_extractdomainname(authority, lines) { 
    for (var i = 0; i < lines.length; i++) { 
     line = lines[i]; 
     alert(line); 
     alert(typeof(line)); 
     if (line.chatAt(0) == '*') { 
      alert('test1'); 
      continue; 
     } 
     if (line.chatAt(0) == '!') { 
      alert('test2'); 
      line.chatAt(0) = ''; 
     } 
     alert('test3'); 
     checkline = sc_checknotasteriskline(authority, line); 
     if (checkline) { 
      domainname = checkline; 
     } 
    } 
    if (!domainname) { 
     for (var i = 0; i < lines.length; i++) { 
      line = lines[i]; 
      alert(line); 
      if (line.chatAt(0) != '*') { 
       alert('test4'); 
       continue; 
      } 
      if (line.chatAt(0) == '!') { 
       alert('test5'); 
       line.chatAt(0) = ''; 
      } 
      alert('test6'); 
      checkline = sc_checkasteriskline(authority, line); 
      if (checkline) { 
       domainname = checkline; 
      } 
     } 
    } 
    return domainname; 
} 

그것은 경고 ' ac ','string ', 그 다음에는 아무것도 없다.

업데이트 :

나는 그 실제 코드가 작동하지 않기 때문에, nsIExtensionManager 및 NSIIOService 연 파일 사이에 차이가 생각하고 있지만,이 테스트 코드가 작동합니다

function makeURI(aURL, aOriginCharset, aBaseURI) { 
    var ioService = Components.classes["@mozilla.org/network/io-service;1"] 
          .getService(Components.interfaces.nsIIOService); 
    return ioService.newURI(aURL, aOriginCharset, aBaseURI); 
} 

URL = makeURI('file://C:/test/TLDs.dat'); 

// URL is a nsIURI; see nsIIOService::newURI for getting a string into a nsIURI. 
var file = URL.QueryInterface(Components.interfaces.nsIFileURL).file; 
// file is now a nsIFile 

// open an input stream from file 
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]. 
         createInstance(Components.interfaces.nsIFileInputStream); 
istream.init(file, 0x01, 0444, 0); 
istream.QueryInterface(Components.interfaces.nsILineInputStream); 

// read lines into array 
var line = {}, lines = [], hasmore; 
do { 
    hasmore = istream.readLine(line); 
    lines.push(line.value); 
} while(hasmore); 

istream.close(); 

// do something with read data 

lines[0].charAt(0); 
+4

JS 문자열은 기본적으로 유니 코드이며 바이트 문자열은 아닙니다. 그래서 그들은 고유 한 인코딩을 가지고 있지 않으며 UTF-8 입력은'charAt' 메소드의 존재 또는 다른 것에 영향을 주어서는 안됩니다.여기 뭔가가 놀고 있습니다 ... 코드를 봅시다! – bobince

+0

bobince, 돌아와! –

답변

1

그것은 어렵다 코드를 보지 않고 어떤 일이 벌어지고 있는지 알려주지 만, 문자열로 평가되는 모든 속성이 실제로는 문자열이 아니라는 것을 기억하십시오. 좋은 예가 location object입니다. 객체를 단독으로 사용하면 문자열 값을 얻을 수 있지만 해당 문자열 값에서 원시 문자열에 사용할 수있는 메소드를 사용할 수는 없습니다.

플러그인이나 ActiveX 컨트롤과 같은 외부 인터페이스에서 제공하는 문자열도 마찬가지입니다. 이 문제에 대한 해결책은 고유의 문자열로 캐스팅하는 것입니다 이러한 방법의

alert((""+window.location).charAt(0));  // auto casting with concatenation 
alert(String(window.location).charAt(0));  // with the String() constructor 
alert(window.location.toString().charAt(0)); // with toString() 

적어도 처음 두 문제 (리셀러 (VAR)와 window.location 교체)를 해결해야한다. 그렇지 않다면 어떤 일이 일어나고 있는지 더 잘 이해할 수 있도록 일부 코드를 게시 해보십시오.


코드를 보면 위의 내용이 정확하다고 가정 할 수 있습니다. readLine 메서드는 비 네이티브 문자열 속성 value을 포함하는 line 개체를 반환합니다. 이는 다소 이상합니다. 다음과 같이 코드를 편집하시는 것이 좋습니다 :

function sc_geteffectivetldnames() { 
    var MY_ID = "[email protected]"; 
    var em = Components.classes["@mozilla.org/extensions/manager;1"]. 
      getService(Components.interfaces.nsIExtensionManager); 
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "chrome/effective_tld_names.dat"); 
    var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]. 
          createInstance(Components.interfaces.nsIFileInputStream); 
    istream.init(file, 0x01, 0444, 0); 
    istream.QueryInterface(Components.interfaces.nsILineInputStream); 
    var line = {}, lines = [], hasmore; 
    do { 
     hasmore = istream.readLine(line); 
     lines.push(String(line.value));  // <--- or ""+line.value 
    } while(hasmore); 
    istream.close(); 
    return lines; 
} 
+0

Andy E, 돌아와! –

+0

@Delirium : 돌아왔다! –