JavaScript allows you to emulate programming patterns and idioms in other languages. It also forms some of its own patterns and idiomatic methods. The more traditional server languages have the object-oriented features that JavaScript has.
Here are a few simple JavaScript object-oriented examples to implement a simple timer.
All examples share this HTML, each example Main.js
<! DOCTYPE html>
1. Method One
function Getrtime () {var EndTime = new Date (' 2017/01/20 00:00:00 '); var nowtime = new Date (); var t = Math.Abs (Endtime.gettime ()-nowtime.gettime ()); var d = 0; var h = 0; var m = 0; var s = 0; if (t >= 0) {d = Math.floor (T/1000/60/60/24); h = Math.floor (t/1000/60/60% 24); m = Math.floor (t/1000/60% 60); s = Math.floor (t/1000% 60); } document.getElementById ("T_d"). InnerHTML = d + "Day"; document.getElementById ("T_h"). InnerHTML = h + "when"; document.getElementById ("T_m"). InnerHTML = m + "min"; document.getElementById ("t_s"). InnerHTML = s + "s"; }//setinterval (Getrtime, 0); var main = function () {} var timer1; Main.prototype.start = function () {timer1 = setinterval (getrtime, 0);} main.prototype.stop = function () {window . Clearinterval (Timer1)} var Test1 = function () {var b = new Main (); B.start (); } var Test2 = function () {var b = new Main (); B.stop (); } 2. Method two
function Getrtime () {var EndTime = new Date (' 2017/01/20 00:00:00 '); var nowtime = new Date (); var t = Math.Abs (Endtime.gettime ()-nowtime.gettime ()); var d = 0; var h = 0; var m = 0; var s = 0; if (t >= 0) {d = Math.floor (T/1000/60/60/24); h = Math.floor (t/1000/60/60% 24); m = Math.floor (t/1000/60% 60); s = Math.floor (t/1000% 60); } document.getElementById ("T_d"). InnerHTML = d + "Day"; document.getElementById ("T_h"). InnerHTML = h + "when"; document.getElementById ("T_m"). InnerHTML = m + "min"; document.getElementById ("t_s"). InnerHTML = s + "s"; }//setinterval (Getrtime, 0); var main = function () {} var timer1; Main.prototype = {start:function () {timer1 = setinterval (getrtime, 0); }, Stop:function () {window.clearinterval (timer1); } }; var Test1 = function () {var b = new Main (); B.start (); } var Test2 = function () {var b = new Main (); B.stop (); } 3. Method three
function Getrtime () {var EndTime = new Date (' 2017/01/20 00:00:00 '); var nowtime = new Date (); var t = Math.Abs (Endtime.gettime ()-nowtime.gettime ()); var d = 0; var h = 0; var m = 0; var s = 0; if (t >= 0) {d = Math.floor (T/1000/60/60/24); h = Math.floor (t/1000/60/60% 24); m = Math.floor (t/1000/60% 60); s = Math.floor (t/1000% 60); } document.getElementById ("T_d"). InnerHTML = d + "Day"; document.getElementById ("T_h"). InnerHTML = h + "when"; document.getElementById ("T_m"). InnerHTML = m + "min"; document.getElementById ("t_s"). InnerHTML = s + "s"; }//setinterval (Getrtime, 0); Function.prototype.method = function (name, FN) {this.prototype[name] = fn;} var main = function () {} var timer1; Main.method (' Start ', function () {timer1 = setinterval (getrtime, 0);}); Main.method (' Stop ', function () {window.clearinterval (timer1);}); var Test1 = function () {var b = new Main(); B.start (); } var Test2 = function () {var b = new Main (); B.stop (); } 4. Method four
function Getrtime () {var EndTime = new Date (' 2017/01/20 00:00:00 '); var nowtime = new Date (); var t = Math.Abs (Endtime.gettime ()-nowtime.gettime ()); var d = 0; var h = 0; var m = 0; var s = 0; if (t >= 0) {d = Math.floor (T/1000/60/60/24); h = Math.floor (t/1000/60/60% 24); m = Math.floor (t/1000/60% 60); s = Math.floor (t/1000% 60); } document.getElementById ("T_d"). InnerHTML = d + "Day"; document.getElementById ("T_h"). InnerHTML = h + "when"; document.getElementById ("T_m"). InnerHTML = m + "min"; document.getElementById ("t_s"). InnerHTML = s + "s"; }//setinterval (Getrtime, 0); Function.prototype.method = function (name, FN) {this.prototype[name] = fn; return this; } var main = function () {} var timer1; Main.method (' Start ', function () {timer1 = setinterval (getrtime, 0);}). Method (' Stop ', function () {window.clearinterval (timer1);}); var Test1 = function () {var b = new Main (); B.start (); } var Test2 = function () {var b = new Main (); B.stop (); }In JavaScript, a function is a class-one object. They can be stored in variables, can be passed as parameters to other functions, can be sent out as return values from other functions, and can be constructed at run time, which provides great flexibility and strong expressive power.
Java Simple Object-oriented