How to recall a value assigned using literal notation?


#1

This is what I understood so far

Constructor Literal
Assign var abc = new Object (); var abc = {};
Adding
Property
var abc = abc.xyz = klask; var abc = {xyz = klask;};
Recall a
Property
abc.xyz; or
abc["xyz"]
Not sure /
should we use the same method that we are using for constructor?

My question is even when we have used literal notation, to recall a property, we have to still use the constructor methods - that is, use either abc.xyz or abc["xyz"]?


#2

@sureshdev,

var james = {
    job: "programmer",
   married: false,
   sayJob: function() {
          // complete this method
          console.log("Hi, I work as a" + this.job);
    }
 };

Description of the james object.
The james object has 3 properties and are seperated by a comma-,

there is a job property with property-key job and it's associated string value 'programmer'
there is a married property with property-key married and it's associated boolean value false
there is a sayJob property with property-key sayJob and it's associated anonymous function VALUE
( they also would 'say', the james-object has the sayJob()-Method )

access via dot-notation

james.job ==> you will get the associated string VALUE of the job property-key, thus
you get the string VALUE 'programmer'

james.married ==> you will get the associated boolean VALUE false

james.sayJob ==> you will get the associated anonymous function VALUE
to call/execute this method you add a pair of parenthesis-( )
james.sayJob();

access via the square-bracket-notation

1 using the literal property-key

james["job"]
james["married"]
james["sayJob"]()

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "job";
var propertyKey2 = "married";
var propertyKey3 = "sayJob";
james[propertyKey1] ==> you will get the associated string VALUE of the job property-key, thus
you get the string "programmer"

james[propertyKey2]
in this case you will get the boolean VALUE of the married property
being false

james[propertyKey3]();
would =display= "Hi, I work as a programmer"

+++++++ this explanation +++++++++++
I defined an extra property in the james object.
in this case a method
as there is a property-key sayJob2
with it's associated value being a function...

var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
              // no PARAMETER used
              // job-Value is picked up from within this Object
              console.log("Hi, I work as a" + " " + this.job);
    },
    sayJob2: function(job) {
              // 1 PARAMETER is used as =local= VARIABLE
              // job will get the  Value as you call providing 1 ARGUMENT
              console.log("Hi, I work as a" + " " + job);
    }
 
 };

 james.sayJob();

 james.sayJob2("xx");

An object has one or more properties separated by a comma-,
A property consists of a property-key and it's associated value.
The associated value can be anything
from an Array [] , null, "string", number, true , _an Object {} or a function.

If the associated value of a property-key is a function
we are calling this property a METHOD.
(if in the course they ask you to create a method you will now know that
you have to create a function within an object using the function-name
as property-key)

Normally if you are using the this keyword in a Method,
you are referring to the property of the object of which it is part of.

So in our case:
james.sayJob();
would translate to
console.log("Hi, I work as a" + " " + "programmer");

A good article about this:
http://stackoverflow.com/questions/80084/in-javascript-why-is-the-this-operator-inconsistent


#3

@sureshdev,

++++ the object as an associative array
++++++++++++++++++++++++++++++++
They were taking a line out of the
developer.mozilla.org manual, look here subject::
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

[[ Begin of quote ]]
Properties of JavaScript objects can
also be accessed or set
using a bracket-notation. ( this in contrast to the = literal-notation = )

Objects are sometimes called associative arrays,
since each property is associated with a string value ( call it the property-key )
that can be used to access it.

Having a myCar object like:

 var myCar = {
          make: "Ford",
         model: "Mustang",
           year: 1969
        };

for example, you could access the properties of the myCar object as follows:
myCar["make"] = "Ford";
myCar["model"] = "Mustang";
myCar["year"] = 1969;
[[ End of quote ]]

The 3 possibilties to get access to the property VALUE

1 Via the so-called DOT-notation, here you use the

literal-property-key-name (NO variable-name allowed)
like:
myCar.year ==wil give you the associated number-VALUE => 1969

2 Via the so-called bracket-notation...

2a ---

We use the literal-property-key-name-as -a-string in brackets
myCar["year"] ==wil give you the associated number-VALUE => 1969

2b ---

We declare a variable which has the property-key-name-as -a-string
assigned to it... like:
**var theKey = "year";
myCar[theKey] ==wil give you the associated number-VALUE => 1969
( They also call this, getting at the -associated- VALUE by reference )