JS 万能变量 var 想要什么是什么
String类 一段字符串可以直接用var保存 也可以用new string(“”)来保存 但string类有更多的方法
对象有好几种创建方式
最简单的一种是
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
也可以写成这样
person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
当你需要创建很多相同类型的对象时就要用到对象构造器
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
//创建对象时
var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");
创建对象的时候 对象定义的内部可以存在function
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
//这里changeName 就相当于一个内部function 创建方式就是这样
this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}
json 的格式是 “name” : “xxx”
等价于js的格式是 name=”xxx”
JSON文件的文件类型是”.json”
数组形式是用[]包括具体内容
"hello" : [
{"name":"JIuzhe","url":"www.xxx.xxx"},
{"name":"BAzhe","url":"www.xxx.xxx"}
// 跟(a,v,b)一样最后一个没有逗号
]
具体在JS中使用方式
var hello= [
{"name":"JIuzhe","url":"www.xxx.xxx"},
{"name":"BAzhe","url":"www.xxx.xxx"}
// 跟(a,v,b)一样最后一个没有逗号
]
hello[0].name;//返回值JIuzhe
//可以直接修改
hello[0].name="九折"
或者
var text='{
"hello" : [ {"name":"JIuzhe","url":"www.xxx.xxx"},
{"name":"BAzhe","url":"www.xxx.xxx"}
// 跟(a,v,b)一样最后一个没有逗号
]
}'
var obj=JSON.parse(text);
//还有var obj=eval("("+text+")");
obj.hello[0].name;//返回值JIuzhe
JSON.parse和eval的区别是eval不检查数据内容是不是符合JSON 而JSON.parse会报错推荐使用者
未完待续