javasrcipt中的对象 Object
javascript中{}可以代表对象
1 javascript已经存在的类型的对象
var v = new Date();
var obj1 = new Object(), obj2 = {};//Object 对象
var arr1 = new Array(), arr2 = [];//Array 对象
var boo1 = new Boolean(true), boo2 = true;//Boolean 对象
var num1 = new Number(123), num2 = 123.45;//Number 对象
var str1 = new String("abc"), str2 = 'abc';//String 对象
2 自定义的对象1:
JSON
var person={firstname:"John", lastname:"Doe", id:5566};
alert(person.firstname);
alert(person.lastname);
alert(person.id);
3 自定义的对象2:
var p = { grade:1, name : "tom", age:27, sex:"男", speak:function(words) { alert(this.name+"说:"+words+"!"); } } p.speak("hello"); 4 自定义的对象3: function Person(name){ this.name = name; this.age = 20; this.say=say; } function say(){ document.write(this.name+":大家好,我今年"+this.age+"岁了"); } var p = new Person("张三"); p.say(); ----------------------------- function Person(name){ this.id = 1; this.name = name; this.age = 20; this.say=function(msg){ document.write(this.name+" 说:"+msg); }; } var p = new Person("张三"); p.say();