转行学开发,代码100天——2018-03-20
对象是变量的容器,是键值对的容器,也是属性和方法的容器。
万物接对象
1.JavaScript中对象定义的方式
方法一:new Object()
//1.new Object()方式
people =new Object();
people.name="Allen";
people.age="28";
people.work="Engeneer";
document.write("name:"+people.name+",age:"+people.age+",work:"+people.work);
方法二:{键值对}
多个键值对之间用,隔开
//2.{键值对} 多个键值对之间用,隔开
var people= {
name:"Allen",
age:"28",
work:"Engeneer"
};
document.write("name:"+people.name+",age:"+people.age+",work:"+people.work);
方法三:函数方法。通过定义函数创建对象。
//3.函数方式创建对象
function people(name,age)
{
this._name = name;
this._age = age;
}
al = new people("Allen","28");
document.write("name:"+al._name+",age:"+al._age);
2、对象属性的调用
对象属性的调用方式有两种,即 .property 或 ["property"]
people.name或
people["name"]
3、对象方法作为一个函数定义存储在对象属性中,
//对象方法作为一个函数定义存储在对象属性中
var people =
{
name:"Allen",
age:"28",
work:"Engeneer",
fullInfo:function()
{
return this.name+this.age+this.work;
}
}
document.write(people.fullInfo());