直接声明对象

var dog = { 
	name: 'Benji', 
	talk: 
		function(){ 
			alert('Woof, woof!'); 
		}
};
alert(typeof(dog));
dog.talk();

这样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());