我如何让原型在 JavaScript 中工作构造函数
how do I get prototype to work of a constructor function in JavaScript
var StartCountdown = function () {
//grab value from the input text field
var minutes = document.getElementById("minutes").value;
//check if the value passed is a number
if (isNaN(minutes)) {
timeDisplay.innerHTML = "Enter a numeric value";
return;
};
//this variable will decrement
secondsRemaining = minutes * 60;
//the ticking function and the milisecond (1)
// calls the method repeatedly
intervalHandle = setInterval(tick, 1000);
//hide the input field once we have the numeric value
// document.getElementById("inputArea").style.display = "none";
StartCountdown.closeInputField;
};
StartCountdown.prototype = {
closeInputField: function() {
document.getElementById("inputArea").style.display = "none";
}
}
我想创建StartCountdown
构造函数的原型函数,以便了解如何正确完成此操作。
这是没有原型的完整版本http://jsfiddle.net/2nXbN/2/
而不是
StartCountdown.closeInputField;
您需要使用 调用该函数
this.closeInputField();
注意:请确保使用 ()
调用函数。没有它们,你只是引用函数,而不是调用它。
您也不必以这种方式创建原型。
创建函数后,只需附加到现有原型即可。
var StartCountDown = function() {
// ...
};
StartCountDown.prototype.closeInputField = function() {
// ...
};
在看了看你的小提琴之后,我也看到了其他几个需要改进的地方。这些只是一些想法供您根据需要使用。
这是一个 jsfiddle 演示
定时器
var Timer = function(elem) {
this.seconds = 0;
this.elem = elem;
this.initialize();
};
Timer.prototype.initialize = function() {
this.timer = document.createElement("span");
this.timer.innerHTML = "0:00";
this.input = document.createElement("input");
this.button = document.createElement("button");
this.button.innerHTML = "Submit";
this.button.addEventListener("click", this.startTimer.bind(this));
this.elem.appendChild(this.timer);
this.elem.appendChild(this.input);
this.elem.appendChild(this.button);
};
Timer.prototype.startTimer = function(event) {
this.removeInput();
this.seconds = parseInt(this.input.value) * 60;
this.interval = window.setInterval(this.countDown.bind(this), 1000);
event.preventDefault();
};
Timer.prototype.stopTimer = function() {
this.updateTimer("Done!");
window.clearInterval(this.interval);
};
Timer.prototype.updateTimer = function(text) {
this.timer.innerHTML = text;
};
Timer.prototype.countDown = function() {
if (this.seconds === 0) {
return this.stopTimer();
}
this.updateTimer(this.timeRemaining(this.seconds));
this.seconds--;
};
Timer.prototype.timeRemaining = function(seconds) {
var minutes = Math.floor(seconds/60),
seconds = seconds % 60,
separator = seconds % 2 === 0 ? ":" : " ";
if (seconds.toString().length < 2) {
seconds = [0, 0, seconds].slice(-2).join("");
}
return minutes + separator + seconds;
};
Timer.prototype.removeInput = function() {
this.input.remove();
this.button.remove();
};
用法
找到一个你想要表现得像计时器的 DOM 元素 ( elem
( 并调用new Timer(elem)
var timer = document.getElementsByTagName("div")[0];
new Timer(timer);
多个定时器
var timers = document.getElementsByTagName("div");
for (var i=0, len=timers.length; i<len; i++) {
new Timer(timers[i]);
}
相关文章:
- 在表单提交将DOM的一部分替换为分部之后,我应该将ajax成功绑定到什么来使我的javascript工作
- 我可以'不要让Javascript工作不正常
- 如何让我的html5功能不兼容警告使用javascript工作
- Excel Web Services 电子邮件 JavaScript 工作表
- 用于Google Fusion Table层的Javascript工作,但试图整理代码
- Bootstrap:如何附加一个类,并且仍然有BS Javascript工作
- JavaScript 工作队列
- JavaScript工作,直到我添加这个&&陈述
- 为什么不'在CasperJS中没有简单的JavaScript工作
- 如何"递归AJAX回调”;在JavaScript工作中
- 基本验证javascript工作不正常
- 需要帮助让这个字符串的Javascript工作
- HTML和JavaScript工作不正常
- 下拉菜单无法从CSS或Javascript工作
- 需要从erb生成一个非常特定的html来让javascript工作
- 如果javascript代码段未使用,它将阻止其他javascript工作
- 将内联事件处理程序移动到chrome扩展的javascript工作表
- 阻止其他Javascript工作的Javascript
- iframe阻止javascript工作
- 无法获取切换函数(JavaScript工作)