1.自定义对象的定义和使用
//1.使用原始的方式创建对象
var obj = new Object();
obj.name = "zhangsan"; //为obj对象添加属性
obj.age = 20;
obj.say = function(){ //添加功能(函数/方法)
console.log(this.name,":",this.age);
}
//测试
console.log(obj.name);
console.log(obj.age);
obj.say()
//2.使用工厂模式创建对象
function createObject(name,age){
var obj = new Object();
obj.name = name; //为obj对象添加属性
obj.age = age;
obj.say = function(){ //添加功能(函数/方法)
console.log(this.name,":",this.age);
}
return obj;
}
//测试
var ob1 = createObject("lisi",20);
console.log(ob1.name);
ob1.say();
var ob2 = createObject("wangwu",25);
console.log(ob2.name);
ob2.say();
//3.使用自定义构造函数创建对象
function Stu(name,age){
this.name = name;
this.age = age;
this.say = function(){
console.log(this.name,":",this.age);
}
}
//测试
var s1 = new Stu("zhaoliu",28);
var s2 = new Stu("xiaoli",21);
s1.say()
s2.say()
//4.直接创建自定义对象
//4.1
var obj={};
obj.name="小白";
obj.say=function () {
console.log("我叫: "+this.name);
};
obj.say();
//4.2最常用
var obj2={
name:"小明",
age:20,
say:function () {
console.log("我叫: "+this.name+"; 年龄: "+this.age);
},
};
obj2.say();
//对象.constructor查看当前对象的构造函数是谁
var a = [10,20,30];
console.log(a.constructor == Array);//推荐数组采用这种方式,因为typeof得到的是object类型
console.log(s1 instanceof Stu);
2.JavaScript内置对象
2.1Array数组
//内置对象--Array数组
var a1 = new Array(); //定义一个空数组
var a2 = new Array(10); //定义一个长度为10数组(值:undefined)
var a3 = new Array(10,20,30); //定义一个指定数值的数组
console.log(a1.length);
console.log(a2.length);
console.log(a3.length);
var a4 = [10,20,30,40,50]; //快捷定义数组
//数组的遍历
for(var i=0; i<a4.length;i++){
console.log(i,"=>",a4[i]);
}
for(var i=a4.length-1; i>=0;i--){
console.log(i,"=>",a4[i]);
}
//只能正序
for(var i in a4){
console.log(i,"=>",a4[i]);
}
//foreach迭代只能正序,不了解语法?
a4.forEach(function(v){
console.log(v);
});
//数组的方法:
console.log(a4.toString());//转换成字符串
console.log(a4.join(":"));//输出时以冒号隔开
var aa = [1,2,3];
console.log(aa.concat(4,5));//两个数组连接,前数组的尾部加后数组的首部
var b = [10,50,30,20,70,40];
console.log(b.join(":"));
console.log(b.sort().join(":"));//从小到大排序
console.log(b.reverse().join(":"));//反转数组
var aa = new Array(10,20,30);
console.log(aa.join(":"));
aa.push(50);//尾部添加数据
aa.push(40);
console.log(aa.join(":"));
aa.pop();//尾部删除数据
console.log(aa.join(":"));
//清空
console.log(b.join(":"));
b.length = 3;//强制只留下数组前3个数据
console.log(b.join(":"));
b = [];//强制清空数组
console.log(b.join(":"));
2.2基本包装类型
//内置对象--基本包装类型 var s1 = 'zhangsan'; console.log(s1.substring(2,5)) //等价于 var s2 = new String("zhangsan"); console.log(s2.substring(2,5));
var num = 20.5678; //等价于 var num = Number(20.5678);//强转 var num = new Number(20.5678);//创建Number对象 console.log(num.toFixed(2));//将数字转换为字符串,传入小数点应保留几位的参数
//布尔值包装类对象 //toString() 把逻辑值转换为字符串并返回结果 //valueOf() 返回布尔对象的原始值 var boo = new Boolean(true); document.write(boo.toString()); <br/> var boo1 = new Boolean(false); document.write(boo1.valueOf());
2.3Date日期
//获取当前日期的函数方法 function formatDate(dd){ if(!dd instanceof Date){ return; } var y = dd.getFullYear(), m = dd.getMonth()+1, d = dd.getDate(), hh = dd.getHours(), mm = dd.getMinutes(), ss = dd.getSeconds(); //判断单位数字前补零操作 hh = hh<10?'0'+hh:hh; mm = mm<10?'0'+mm:mm; ss = ss<10?'0'+ss:ss; return y+"-"+m+"-"+d+" "+hh+":"+mm+":"+ss; } var dd = new Date(); //从1970年1月1日零时零分0秒至今的毫秒数 document.write("时间戳:"+dd.valueOf());
document.write("<h2>"+formatDate(dd)+"</h2>");
2.3String字符串
var s1 = "ZhangSanFeng"; document.write(s1+"<br/>"); document.write(s1[5]+"<br/>");//获取第5个位置的字符 document.write(s1.charAt(5)+"<br/>");//获取第5个位置的字符 document.write(s1.substr(5,3)+"<br/>");//截取从索引5开始后3个 document.write(s1.substring(5,8)+"<br/>");//截取5.6.7三个索引位置的字符 document.write("小写:"+s1.toLowerCase()+"<br/>"); document.write("大写:"+s1.toUpperCase()+"<br/>"); //子串查找 document.write(s1.indexOf("an")+"<br/>");//从前往后查找 document.write(s1.lastIndexOf("an")+"<br/>");//从后往前查找 //替换 document.write(s1.replace("SanFeng","WuJi")+"<br/>");
//逗号全部替换 document.write("10,20,30,40".replace(/,/g,":")+"<br/>");
var str = "10:23:45:67:87:65"; console.log(str); console.log(str.split(":"));//根据冒号的分割组成数组Array
var s2 = " zhangsan "; console.log("#"+s2+"#"); console.log("#"+s2.trim()+"#");//去除空格字符
2.4Math对象
console.log(Math.abs(-20)); console.log(Math.ceil(10.000000001)); //进一取整 console.log(Math.floor(10.9999999)); //舍去取整 console.log(Math.round(10.5)); //四舍五入取整 console.log(Math.max(10,20)); //最大 console.log(Math.min(10,20)); //最小
console.log(Math.random()) //0~1的随机数 console.log(Math.ceil(Math.random()*1000000)%10+1) //1~10的随机数