Java Simple Object-oriented

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.