So far I saw three ways for creating an object in JavaScript. Which way is best for creating an object and why?
I also saw that in all of these examples the keyword var
is not used before a property — why? Is it not necessary to declare var
before the name of a property as it mentioned that properties are variables?
In the second and third way, the name of the object is in upper-case whereas in the first way the name of the object is in lower-case. What case should we use for an object name?
function person(fname, lname, age, eyecolor){ this.firstname = fname; this.lastname = lname; this.age = age; this.eyecolor = eyecolor; } myFather = new person("John", "Doe", 50, "blue"); document.write(myFather.firstname + " is " + myFather.age + " years old.");
var Robot = { metal: "Titanium", killAllHumans: function(){ alert("Exterminate!"); } }; Robot.killAllHumans();
var NewObject = {}; NewObject['property1'] = value; NewObject['property2'] = value; NewObject['method'] = function(){ /* function code here */ }
Using an Object Literal This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object in one statement. An object literal is a list of name:value pairs (like age:50) inside curly braces {}.
Objects can be initialized using new Object() , Object. create() , or using the literal notation (initializer notation). An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ( {} ).
An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.
There is no best way, it depends on your use case.
Person
(you should start the name with a capital letter) is called the constructor function. This is similar to classes in other OO languages.Update: As requested examples for the third way.
Dependent properties:
The following does not work as this
does not refer to book
. There is no way to initialize a property with values of other properties in a object literal:
var book = { price: somePrice * discount, pages: 500, pricePerPage: this.price / this.pages };
instead, you could do:
var book = { price: somePrice * discount, pages: 500 }; book.pricePerPage = book.price / book.pages; // or book['pricePerPage'] = book.price / book.pages;
Dynamic property names:
If the property name is stored in some variable or created through some expression, then you have to use bracket notation:
var name = 'propertyName'; // the property will be `name`, not `propertyName` var obj = { name: 42 }; // same here obj.name = 42; // this works, it will set `propertyName` obj[name] = 42;
There is various way to define a function. It is totally based upon your requirement. Below are the few styles :-
var person = new Object(); person.name = "Anand", person.getName = function(){ return this.name ; };
var person = { name : "Anand", getName : function (){ return this.name } }
function Person(name){ this.name = name this.getName = function(){ return this.name } }
function Person(){}; Person.prototype.name = "Anand";
function Person(name){ this.name = name; } Person.prototype.getName = function(){ return this.name }
var person = new function(){ this.name = "Anand" }
You can try it on console, if you have any confusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With