@mathewvirgen
james job married explained
- - - B a s i c s - - -
Each Object has one or more properties.
Each property consists of a property-key and itâs associated value.
var object1 = {
name: "First"
}
So object1 has 1 property with
a type property-key name
and itâs associated value âFIRSTâ (which in this case is a âstring-valueâ)
OR
var myObj = {
type: 'fancy',
disposition: 'sunny'
}
myObj has 2 properties seperated by a comma-,,
a property with property-key type and an associated-string-value âfancyâ
a disposition-property with property-key disposition and
âŚan associated-string-value âsunnyâ.
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
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 which 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