2011-01-10 11 views
1

次の関数では、 '.prototype'部分を取り出すと、関数は機能しなくなります。なぜコード '.prototype'のスニペットを取り出すと、ここでJavaScriptが動作しなくなるのですか?

imageClock.display.prototype.update = function() { 
var dateObj = new Date(); 
var currentTime = dateObj.getHours() + ":" + dateObj.getMinutes() + ":" + dateObj.getSeconds(); 
var currentTimeHTML = imageClock.imageHTML(currentTime) + '<img src="' + ((dateObj.getHours() >=12) ? imageClock.digits[11] : imageClock.digits[10]) + '" />'; 
document.getElementById(this.spanId).innerHTML = currentTimeHTML; 
} 

私が理解できないことは、プロトタイプフレームワークをコードの一部として使用していないことです。私のhtmlファイルがリンクしている唯一のファイルは、このコードを含むjavascriptファイルです。 (下の完全なコード;ファイルの名前は、JavaScriptのみが私のhtmlファイルのポイント/リンクへのファイルでのJavaScript 'と呼ばれるフォルダ、内部clock3.jsです)

var imageClock = new Object(); 

imageClock.digits = ["images/clock/0.gif", "images/clock/1.gif", "images/clock/2.gif", "images/clock/3.gif", "images/clock/4.gif", "images/clock/5.gif", "images/clock/6.gif", "images/clock/7.gif", "images/clock/8.gif", "images/clock/9.gif", "images/clock/am.gif", "images/clock/pm.gif", "images/clock/colon.gif"]; 

imageClock.instances = 0; 

var preLoadImages = []; 

// preloads javascript images 
for (var i = 0; i < imageClock.digits.length; i++) { 
preLoadImages[i] = new Image(); 
preLoadImages[i].src = imageClock.digits[i]; 
} 

imageClock.imageHTML = function(timeString) { 
var sections = timeString.split(":"); 

// makes the hour digit either 12pm or 1pm depending on what time it is so that 0am or 13pm doesn't appear 
if (sections[0] == "0") { 
    sections[0] = "12"; 
} else if (sections[0] >= 13) { 
    sections[0] = sections[0] - 12 + ""; 
} 
for (var i = 0; i < sections.length; i++) { 
    if (sections[i].length == 1) { 
    sections[i] = '<img src="' + imageClock.digits[0] + '" />' + '<img src="' + imageClock.digits[parseInt(sections[i])] + '" />'; 
    } else { 
    sections[i] = '<img src="' + imageClock.digits[parseInt(sections[i].charAt(0))] + '" />' + '<img src="' + imageClock.digits[parseInt(sections[i].charAt(1))] + '" />'; 
    } 
} 

// section[0] indicates hours, the image tag is the colons, sections[1] indicates the second part which is the minutes, and sections[2] indicates seconds. delete sections[2] and the seconds part of the clock will disappear. 
return sections[0] + '<img src="' + imageClock.digits[12] + '" />' + sections[1] + '<img src="' + imageClock.digits[12] + '" />' + sections[2]; 
} 

imageClock.display = function() { 
var clockInstance = this; 
this.spanid = "clockSpan" + (imageClock.instances++); 
document.write('<span id = "' + this.spanId + '"></span>'); 
this.update(); 
setInterval(function() {clockInstance.update()}, 1000); 
} 

imageClock.display.prototype.update = function() { 
var dateObj = new Date(); 
var currentTime = dateObj.getHours() + ":" + dateObj.getMinutes() + ":" + dateObj.getSeconds(); 
var currentTimeHTML = imageClock.imageHTML(currentTime) + '<img src="' + ((dateObj.getHours() >=12) ? imageClock.digits[11] : imageClock.digits[10]) + '" />'; 
document.getElementById(this.spanId).innerHTML = currentTimeHTML; 
} 

と、このJavaScriptを使用してhtmlファイルコードは、あなたが、私はプロトタイプのフレームワークが含まれていない見ることができるように

<html> 
<head> 
<script type="text/javascript" src="javascript/clock3.js"></script> 
<title>Javascript Clock</title> 
</head> 
<body> 
<h1>The Current Time:</h1> 
<p><script type="text/javascript">new imageClock.display();</script></p> 
</body> 
</html> 

を次のようにシンプルな時計がある表示するには、私は「.prototype」部分を取り出したときにコードが動作を停止し、なぜ私は理解していない

答えて

4

「prototype」という単語は、Javaの少なくとも2つの興味深い方法で使用されていますスクリプトの世界:

  • それは見つけるためのランタイムが使用するFunctionインスタンスのプロパティの名前は、オブジェクトインスタンスのプロパティ

を「継承」の人気フレームワーク(プロトタイプ)

  • の名前ですFunctionインスタンスの「プロトタイプ」属性は、ここに関わるものです。 Prototypeフレームワークとは何の関係もありません。

    コードがFunctionインスタンスの "prototype"オブジェクト(ここでは、 "display"関数自体が "imageClock"オブジェクトのプロパティによって参照される)にプロパティを追加すると、オブジェクトインスタンスが構築されます

    new imageClock.display(/* whatever */) 
    

    すべてのコードで「更新」機能が利用できます。あなたのコードは、そのタイマーハンドラでその事実を使用します。

  • 2

    Prototype枠組みとは何の関係もありませんprototypeプロパティ。

    prototypeプロパティはJavascriptの不可欠な部分です。オプションのキーワードではありません。
    prototypeあなたはここを参照してください(非常に悪いIMOの名前)を行うには全く何もJavaScriptライブラリ「プロトタイプ」ではありませんdocumentation

    2

    を参照してください。

    Javascript言語は、プロトタイプベースの継承を使用しています(クラスベースの継承とは対照的に、多くの人が慣れています)。あなたはそれについての詳細を読むことができますon wikipedia

    関連する問題