3

I have a javascript object like this

 var student = function () {
     this.id = 1;
     this.Name = "Shohel";
     this.Roll = "04115407";
     this.Session = "03-04";
     this.Subject = "CSE";
 };

and i have a javascript array list like this

var students = [];

now i want to push student into students, this is shown below

students.push(new student())  //no prolem

students.push(new student[id = 3]) //Problem

here second line occurs exceptions, how can i push javascript object like as c# add list, which is representing second line? thanks

5
  • 2
    So what are you expecting when you try to call a function with brackets and not parenthesis ? Commented Jun 9, 2014 at 3:13
  • I want to set default value of student when i push into students Commented Jun 9, 2014 at 3:16
  • You have not provided for any way to pass values into your object class. Commented Jun 9, 2014 at 3:18
  • but i can do the same work in c# , without providing parameter, is it possible in javascript? Commented Jun 9, 2014 at 3:24
  • JavaScript is not C#. They are different languages and work differently. If you can something like this in C# then the answer is no, you can't do that in JavaScript. I really recommend to read a JavaScript tutorial if you intend to use this language: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide . Commented Jun 9, 2014 at 3:30

2 Answers 2

8

You simply can't, what you can do though is accept a config as a parameter to your constructor and read it like this

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";
            };

And call it like this

students.push(new student({id: 3}));

EDIT, PREFERRED ONE

Just as adeneo pointed out if you want to get rid of the repititive || for default values, you can use jQuery to pass them

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;
                };
Sign up to request clarification or add additional context in comments.

10 Comments

this answer I was waiting... ;) +1
as a sidenote, you can remove most of those ugly OR's by just defining the object in config = config || {id:1, Name:'Shohel' ...}
I thought of that but if config is actually defined and all of the parameters are not defined you can get ugly values, i.e. undefined.
If that's an issue, the question is tagged jQuery, so just do config = $.extend(config, defaults) etc. and you can remove all the ugly OR's
Okay can you be a bit more precise ? What error or behaviour do you get ?
|
6

Make the values that are variable parameters of the function. For example:

var Student = function (id) {
  this.id = id;
  // ...
};

students.push(new Student(3));

I recommend to read a JavaScript tutorial about functions:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.