构造函数
/*
*
*构造函数的方法
* 1.自定义构造函数
* 2.工厂模式构造函数
* 3.系统自带构造函数
* */
// 1.系统自带构造函数
var chenshaoyang = new Object();
chenshaoyang.name = "陈少扬";
console.log(chenshaoyang.name);
chenshaoyang.age = 10;
console.log(chenshaoyang.age);
chenshaoyang.infomation = function () {
console.log("我的名字叫做" + this.name + "我今年"+ this.age + "岁了");
};
chenshaoyang.infomation();
//2.工厂模式构造函数
function person() {
var obj = Object();
obj.name = "陈少扬";
obj.age = 10;
obj.infomation = function(){
console.log("我的名字叫做" + obj.name + "我今年" + obj.age + "岁了");
};
return obj;
}
var chenshaoyang = person();
console.log(chenshaoyang.name);
console.log(chenshaoyang.age);
chenshaoyang.infomation();
//3.自定义构造函数
function Person() {
this.name = "陈少扬";
this.age = 10;
this.information = function () {
console.log("我的名字叫做" + this.name + "我今年" + this.age + "岁了");
}
}
var chenshaoyang = new Person();
// console.log(chenshaoyang["name"]);
// chenshaoyang["information"]();
// for(var key in chenshaoyang){
// console.log(key);
// console.log(chenshaoyang[key]);
// }
console.log(chenshaoyang.name);
console.log(chenshaoyang.age);
chenshaoyang.information();
/*
* 作业:
* 1.创建一个图片的对象,图片有宽,有高,有大小(4M),图片可以展示内容
* 2.创建一个小猫的对象,猫有颜色,体重,年龄,大小,小猫可以抓耗子,可以打架。
* */
var image = new Object();
image.width = 300;
image.height = 500;
image.size = "4M";
image.content = function () {
console.log("图片的宽为:" + this.width + "图片的高为:" + this.height + "图片的大小为:" + this.size);
};
console.log(image.width);
console.log(image.height);
console.log(image.size);
image.content();
var cat = new Object();
cat.color = "yellow";
cat.weight = 40;
cat.size = 156;
cat.cando = function () {
console.log("我是猫,我可以打架,可以抓耗子");
};
console.log(cat.color);
console.log(cat.weight);
console.log(cat.size);
cat.cando();