我有一个像这样的javascript对象
var student = function () {
this.id = 1;
this.Name = "Shohel";
this.Roll = "04115407";
this.Session = "03-04";
this.Subject = "CSE";
};
我有一个像这样的javascript数组列表
var students = [];
现在我想把学生推向学生,如下所示
students.push(new student()) //no prolem
students.push(new student[id = 3]) //Problem
这里第二行出现异常,如何将javascript对象推送为c#add list,这是代表第二行?感谢
答案 0 :(得分:8)
你根本无法做到,你可以做的是接受一个配置作为你的构造函数的参数并像这样阅读
var student = function (config) {
config = config || {};
this.id = config.id || 1;
this.Name = config.Name || "Shohel";
this.Roll = config.Roll || "04115407";
this.Session = config.Session || "03-04";
this.Subject = config.Subject || "CSE";
};
并像这样称呼它
students.push(new student({id: 3}));
编辑,首选
就像adeneo指出你想要删除重复||
的默认值一样,你可以使用jQuery传递它们
var student = function (config) {
var defaults = {
id: 1,
Name: "Shohel",
Roll: "04115407",
Session: "03-04",
Subject: "CSE"
};
config = $.extend(defaults, config || {});
this.id = config.id;
this.Name = config.Name;
this.Roll = config.Roll;
this.Session = config.Session;
this.Subject = config.Subject;
};
答案 1 :(得分:6)
创建函数的变量参数的值。例如:
var Student = function (id) {
this.id = id;
// ...
};
students.push(new Student(3));
我建议您阅读有关函数的JavaScript教程: