31 Introdução a Objetos I Propriedades


#1

Não sei o que acontece
A instrução é a seguinte: Faça a variável global species ser a espécie de snoopy e a variável age ser sua idade. Para uma, use notação de ponto, e para a outra use notação de colchetes!

var snoopy = new Object();
snoopy.species = “beagle”;
snoopy.age = 10;

// salvar a idade e a espécie de Snoopy em variáveis
// usar dot notation para a espécie de snoopy
var species = “beagle”;

// usar bracket notation para a idade de snoopy
var age = snoopy[age];

Oops, tente outra vez. Parece que você não salvou a idade de snoopy em age!


#2

The =short answer=
If you write
var age = snoopy[age];
the Javascript interpreter
will look at the =age= in snoopy[age] as being a variable
but you have not yet used a

var age="age";

so for the interpreter age is invalid…

Try to use snoopy["age"]


- - - 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 separated 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 separated 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
(no variable allowed, only literal property-key-name )

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”


#3

Thank you.
Resolvi o problema.