Creating / populating a custom Javascript object

I created an ashx page that will serve me as an XML document full of basic user information. I'm not sure if this is the best way to create and populate your custom javascript object. I saw how they were created in two ways:

function User() { this.Id; this.FirstName; this.LastName; this.Title; } 

and

 var User2 = { Id: null, FirstName: null, LastName: null, Title: null } 

I could fill each of them by doing something like:

 //first object User.Id = 1 //second object User2.FirstName = 'John' 

Is one way to create an object better than another?

Edit : a year and a half later, I saw that this question received a popular mark, so I just would like to note that today I am using Crockford module template .

+7
javascript object
source share
3 answers

You cannot access the first properties of an object without instantiating it, i.e. using the new keyword:

  var myUser = new User() ; document.write(myUser.id) ; 

The second object is the object literal, which is available without an instance, since it was already created during analysis.

The difference comes into play if you want to use prototypical inheritance to create a new object based on the old one. Writing an object literal is probably easier to understand and a more appropriate template if you have a fairly compact code base. However, prototyping comes in handy if you want to create a new object by adding an existing object to another object without overwriting the magnified object:

 ipUser.prototype = User ; ipUser.ip = "128.0.0.1" ; 

In your case, this difference may not seem impressive, but you have to imagine how much redundant code you get if you created a different object literal for each small addition to the original object.

Take a look at the Mozilla Developer Center page on JavaScript objects, if you have additional questions, this is very well defined: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Global_Objects:Object .

Hth

+8
source share

You do not need to create an object with empty values ​​first. JavaScript does not need place owners and can dynamically add properties and methods at any time. That is, this would be enough:

 var User2 = {}; User2.Id = 1; User2.FirstName = 'John'; //...Etc. 

If you are just concerned about data storage, I would use this form (object literal, i.e. your second method).

Update. You can also make things a little easier and create a function that creates custom objects for you:

 function createUser(id, firstname, lastname, title) { return { Id: id, FirstName: firstname, LastName: lastname, Title: title }; } var User2 = createUser(1, 'John', 'Smith', 'Manager'); 
+5
source share

The first is a typical object known from OOP. You can add functions for actions on such attributes (provided that the getFullName function exists):

 var u = new User(); u.getFullName(); 

The second is just an associative array in which strings are mapped to values.

In JavaScript, the boundary between them is not as strict as in other programming languages.

+1
source share

All Articles