JavaScript Object Literal at first glance rating not rated
SoftXML
                  Programming Books
                Open Web Directory
           SoftXMLLib | SoftEcartJS
xml products   Home page    contact    site map
Contact Us
Latest Blog Articles:   Latest SEO Articles | RSS Feed
DOM manipulation best practices
JavaScript Module Pattern
How to start developing with the Nextjs
Angular application structure
Write Guest Articles
Articles Archive




Loading...

The Art & Science of JavaScript ($29 Value FREE For a Limited Time)
The Art & Science of JavaScript ($29 Value FREE For a Limited Time)









JavaScript Object Literal at first glance


The ability to define objects in the form of literals is available in JavaScript, provides a shorter, more expressive and a less error prone way to write object definitions.

JavaScript objects can be thought of as hash tables, containing key-value pairs (resembling constructs that in other languages are called "associative arrays"). Values can be data of simple types or other objects - in both cases they are called properties. In addition, the values may be functions, in which case they are called methods.

Objects that you create yourself (in other words, user-defined language objects) are constantly available for change. Many of the properties of such objects are also editable.

You can start by creating an empty object, and then gradually add new functionality to it. Object Definition in the form of literals is ideal for creating such gradually expandable objects.

Take a look at the following example (you can change the code in the sandbox below and then press execute button):

In the previous example, we started creating an object from scratch, creating an empty object. Then we added a property and method to it. At the any stage of the program you can:

• Change properties and methods, for example:

• Remove properties and methods:


delete dog.name;
• Add additional properties and methods:

dog.say = function () {
 return “Woof!”;
};
dog.fleas = true;

Starting with an empty object is optional. Literal Objects allow you to add the necessary functionality at the creation stage, as shown in the following example.


var dog = {
 name: “Benji”,
 getName: function () {
 return this.name;
 }
};

JavaScript doesn`t actually have empty objects. Even the simplest object {} already has properties and methods inherited from Object.prototype. By "empty" we mean an object that does not have other properties than those inherited.

Tag cloud

'Benji'; doggetName

()

available

called creating empty

example

example var

form

function

functionality

gradually

inherited

literals

method

methods

object objects properties

stage

{  return

{}; dogname

Rate This Article
(votes 7)

No Comments comments

Post Comment

We love comments on this blog - they are as important as anything we write ourself. They add to the knowledge and community that we have here. If you want to comment then you�re more than welcome � whether you feel you are a beginner or an expert � feel free to have you say.



* = required
Leave a Reply
Name *:
Email *
(will not be published):
Website:
Comment *:
Human Intelligence Identification *:
What is the background color of this web page?
  
Close
Please enter a valid email Please enter a valid name Please enter valid email Please enter valid name Enter valid year
™SoftXML.   Privacy Statement  |  Article Archive  |  Popular Web Development Books
^Top