JavaScript(JavaScript物件一)
阿新 • • 發佈:2018-12-22
鏈式程式設計:
function Car(name, color) { this.name = name;//屬性=值 this.color = color; this.show = function () {//方法=function () {} alert(name + color); return this; } this.speed = function (speed) { alert("時速" + speed + "KM"); return this;//返回當前物件 } } //可通過 對此方法進行擴充套件(新增其他屬性) Car.prototype.money = function () { alert("230W"); return this; } //窗體載入時呼叫當前物件中的方法 window.onload = function () { //1. new Car("奧迪", "黑色").show(); // new Car().speed(800); // new Car().money(); var car = new Car("奧迪", "黑色"); //鏈式程式設計:每次呼叫之後都返回它本身 car.money().show();//可以隨意呼叫方法/屬性 }
日期迴圈:
window.onload = function () { //showtime(); setInterval(showtime, 5000); //定時->間隔指定時間 迴圈執行 setTimeout(showtime, 1000);//定時->等待執行時間,執行一次 } function showtime() { var date = new Date(); var year = date.getFullYear(); var mouth = date.getMonth(); mouth = mouth > 9 ? mouth : ("0" + mouth); var da = date.getDate(); da = da > 9 ? da : ("0" + da); var hour = date.getHours(); hour = hour > 9 ? hour : ("0" + hour); var min = date.getMinutes(); min = min > 9 ? min : ("0" + min); var sec = date.getSeconds(); sec = sec > 9 ? sec : ("0" + sec); var Div = document.getElementById("div"); div.innerHTML = (year + "年" + mouth + "月" + da + "日" + hour + ":" + min + ":" + sec); }