次の関数では、 '.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」部分を取り出したときにコードが動作を停止し、なぜ私は理解していない