1,对象字面量创建对象
var obj = {
uname : 'jack',
age : 18,
sex : '男',
adress: 'china',
telephone: '110',
skill: function() {
console.log('喷火');
}
}
// 调用
console.log(obj.uname);
console.log(obj.sex);
obj.skill();
2, 用new object 创建对象
var obj = new Object()
obj.uname = 'jack';
obj.age = 19;
obj.sex = '男';
obj.adress = 'china';
obj.telephone = '110';
obj.skill = function() {
console.log('喷火');
}
console.log(obj.uname);
console.log(obj.adress);
obj.skill();
3,构造函数创建对象
function Obj(uname,age,sex,adress) {
this.uname = uname;
this.age = age;
this.sex = sex;
this.adress =adress;
this.skill = function(fire) {
console.log(fire);
}
}
// 调用
var jack = new Obj('jack',19,'男','china');
console.log(jack.uname);
console.log(jack['age']);
jack.skill('喷火')
var jane = new Obj('jane',18,'女','china');
console.log(jane.age);
console.log(jane['adress']);
console.log(jane.sex);
jane.skill('抓狗');
构造函数创建两个英雄对象
function Hero(name,type,blood,attack) {
this.uname = name;
this.type = type;
this.blood = blood;
this.attack = function(style) {
console.log(style);
}
}
var lianpo = new Hero('廉颇','坦克',500);
console.log(lianpo.uname);
console.log(lianpo.blood);
lianpo.attack('近战')
var houyi = new Hero('后羿','射手',300,);
console.log(houyi.type);
console.log(houyi['blood']);
houyi.attack('远程');
创建一个电脑对象,有颜色 品牌 重量等属性
~用字面量创建
var obj = {
color: 'red',
weight: '900g',
brand: 'mi',
system: 'win10',
fn: function () {
console.log('看电影');
console.log('听音乐');
console.log('打游戏');
console.log('敲代码');
}
}
console.log(obj.brand);
obj.fn();
~用new object 创建
var obj = new Object();
obj.col = 'red';
obj.wei = '900g';
obj.bra = 'mi';
obj.sys = 'win10';
obj.fn = function() {
console.log('看电影 打游戏 敲代码');
}
console.log(obj.col);
console.log(obj['sys']);
obj.fn();
~用构造函数创建
function Obj (color,weight,brand,system) {
this.col = color;
this.wei = weight;
this.bra = brand;
this.sys = system;
this.fn = function (fn1) {
console.log(fn1);
}
}
var xiaomi = new Obj('red',900,'mi','win10');
console.log(xiaomi.col);
console.log(xiaomi['wei']);
xiaomi.fn('打游戏 听音乐 看电影');
创建一个按钮对象,要有长 宽 高 背景颜色 和点击行为
~对象字面量创建
var obj = {
width: '20cm',
height: '16cm',
bgc: 'red',
cli: function () {
console.log('滴滴');
}
}
console.log(obj.width);
console.log(obj.height);
console.log(obj.bgc);
obj.cli();
~new Object 创建
var obj = new Object;
obj.wid = '20cm';
obj.hei = '16cm';
obj.bgc = 'red';
obj.cli = function() {
console.log('滴滴');
}
console.log(obj.wid);
console.log(obj.hei);
console.log(obj.bgc);
obj.cli();
~构造函数创建
function Obj(width,height,bgcolor) {
this.wid = width;
this.hei = height;
this.bgc = bgcolor;
this.cli = function(fn1) {
console.log(fn1);
}
}
var but1 = new Obj('20cm','16c','red');
var but2 = new Obj('30cm','28cm','yellow');
console.log(but1.wid);
but1.cli('滴滴');
console.log(but2['bgc']);
but2.cli('哒哒');
创建一个车的对象,要有重量,颜色,牌子,有拉货,载人,和躲雨功能
~用字面量创建
var obj = {
wei: 4,
col: 'red',
bra: 'benz',
oth: function () {
console.log('拉货,载人,躲雨');
}
}
console.log(obj.wei);
console.log(obj.col);
console.log(obj.bra);
obj.oth();
~new Object创建
var obj = new Object();
obj.wei = 4;
obj.col = 'red';
obj.bra = 'benz';
obj.oth = function() {
console.log('载人,拉货,躲雨');
}
console.log(obj.wei);
console.log(obj.col);
console.log(obj.bra);
obj.oth();
~构造函数创建
function Obj(wei,col,bra) {
this.wei = wei;
this.col = col;
this.bra = bra;
this.oth = function (oth) {
console.log(oth);
}
}
var car = new Obj(4,'red','benz');
console.log(car.wei);
console.log(car.col);
console.log(car.bra);
car.oth('载人,拉货,躲雨');
遍历对象
var obj = {
name: 'jack',
age:22,
sex: '男',
phone: 110,
}
for(var k in obj) {
console.log(k);
console.log(obj[k]);
}