javascript 封装(给自己看)

时间:2023-03-08 19:28:52
javascript 封装(给自己看)

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>时间日期</title>
<meta charset="utf-8" />
<script src="setTime.js"></script>
</head>
<body>
<script> //封装1
var mc = new MyClass();//初始化组件
mc.setTime("2015-12-04",1,2);//组件调用方法 //封装2
Person("zhang",18,"web","什么是封装"); </script>
</body>
</html>
setTime  JS文件
//封装一
//设置跑马灯效果
function MyClass(){
this.setTime=function(z_str,num1,num2){//定义类方法
var date=new Date(); //当前时间
var tYear=date.getFullYear(); //当前年份
var tMonth=date.getMonth(); //当前月份
var tDay=date.getDate(); //当前日期
console.log("当前时间="+tYear+"-"+(tMonth+1)+"-"+tDay); //日期对象 month+1
var iTime=new Date(z_str); //目标日期
var iYear=iTime.getFullYear(); //目标年份
var iMonth=iTime.getMonth(); //目标月份
var iDay=iTime.getDate(); //目标日期
console.log("目标时间="+iYear+"-"+(iMonth+1)+"-"+iDay); if(tYear < iYear){
console.log("当前年份小于目标年份");
document.write("<script src=\"z_marquee.js\"><\/script>");
}else if(tYear==iYear){
console.log("当前年份等于目标年份");
if(tMonth <= iMonth){
if(tDay<iDay){
console.log("当前日期小于目标日期");
document.write("<script src=\"z_marquee.js\"><\/script>");
}else{
console.log("当前日期大于目标日期");
} }else{
console.log("到达预期时间!");
}
}else{
console.log("当前年份等大于目标年份");
}
console.log("效果="+num1);
console.log("效果="+num2);
}
} //封装二
function Person(name,age,job,sn){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(str){
document.write(str);
};
this.sayName(sn);
}
//封装三

以上均为自己备份 如有不足地方 望大家指正