var obj={ 属性名:属性值, 方法名:function(){ //函数执行体 } }
var obj=new Object(); obj.属性名=属性值; obj.方法名=function(){ //函数执行体 }
function test([参数列表]){ this.属性名=属性值; this.方法名=function(){ //函数执行体 } } var obj=new test(参数);
function createObject(nam,age){ var obj=new Object(); obj.name=name; obj.age=age; obj.run=function(){ return this.name+this.age }; return obj; } var obj1=createObject(‘zhangsan’,100); var obj2=createObject(‘lisi’,200);
function test(){ test.prototype.属性名=属性值; test.prototype.方法名=function(){ //函数执行体 } } var obj=new test();
function test(参 1,参 2){ this.属性名 1=参 1; this.属性名 2=参 2; } test.prototype.方法名=function(){ //执行代码 } var obj=new test(参 1,参 2);
var obj={ name:"Zhangsan", age:"18", action:function(){ alert("Hello!"); } } for (var i in obj){ console.log(i); //控制台依次显示:name age action } for (var i in obj){ console.log(obj[i]); //控制台依次显示:Zhangsan 18 ƒ (){alert("Hello!");} alert("Hello!"); } }
var obj={} obj.name="Zhangsan"; obj.age="18"; obj.action=function(){ alert("Hello!"); }