2012-04-09 9 views
1

を意味します。私はJavaScriptコードを解析しています。特に、document.createElementのような動的関数は正規表現で解析しています。しかし私はフックを作るためにdocument.createElementを包むことが提案されました。Domオブジェクトのフックは、私のプロジェクトで

var f = document.createElement; 
document.createElement = function(tagName){ 
    console.log(tagName); 
    f.apply(document, arguments); 
} 

このコードは、誰も私を助けることができる私は、これは私のコードで使用することができる方法を理解することができませんdocument.createElement

を追跡している:私は、彼らはまた、例を提供

を行う方法を理解していないのです

答えて

2

このコードでは、元のdocument.createElementへの参照が格納され、新しい機能を指すようにdocument.createElementを再割り当てします。この新機能のインサイド

、それは最初の引数を記録し、その後、彼らがいたとして、元document.createElement()はそのthis値としてdocumentを渡すとargumentsの残りを渡して呼び出します。短い、 `ドキュメントでここ

console.log()が利用できない場合があります場合は、あなたがにその行を変更したい場合があり

(function() { 
    var documentCreateElement = document.createElement; 

    document.createElement = function(tagName) { 
     console.log(tagName); 
     return documentCreateElement.apply(document, arguments); 
    } 

})(); 

...私はそれをコーディングする方法をある...

window.console && console.log && console.log(tagName); 
+0

.createElement'は "カスタマイズされました" – Joseph

+0

この関数の出力は何ですか?これをWebページのすべてのdocument.createElement関数をトラッキングするためにどのように使用できますか? – user1275375

+0

@ user1275375 'document.createElement() '呼び出しごとに最初の引数が記録されます。 – alex

関連する問題