直接声明对象
var dog = {
name: 'Benji',
talk:
function(){
alert('Woof, woof!');
}
};
alert(typeof(dog));
dog.talk();
也可以用构造函数
//--------构造函数--------
function Hero() {
this.occupation = 'Ninja';//职业是武士
}
var hero = new Hero();//创建新对象
alert(hero.occupation);//调用属性//---------带参的构造函数-----------
function Hero(name) {
this.name = name;
this.occupation = 'Ninja'; //职业是武士
//行为
this.whoAreYou = function() {
return "I'm " + this.name + " and I'm a " + this.occupation;
}
}
var h1 = new Hero('Michelangelo');
var h2 = new Hero('Donatello');
alert(h1.whoAreYou()); 原文:http://blog.csdn.net/zhengwei223/article/details/41684697