問題如下,已經完成單個對象的簡單應用,希望定義一個數組,能包含多個student。
復制代碼 代碼如下:var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);
方法一:
復制代碼 代碼如下:var students = [];
students[students.length] = new Object();
students[students.length].name = "Lanny";
students[students.length].age = "25";
students[students.length].location = "China";
var json = JSON.stringify(students);
方法二:
復制代碼 代碼如下:var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
students = (students || []).push(student);
var json = JSON.stringify(students);
查看更多JavaScript的語法,大家可以關注:《JavaScript 參考教程》、《JavaScript代碼風格指南》,也希望大家多多支持。