6/6 Difference with code


#1
HI there.
(pre p.s.: First of all my apologizes for my bad english.)

I've read some threads here and i dont understand why some guys tells this code is right:

> function add(firstName, lastName, email, phoneNumber) {
>             contacts[contacts.length] = {
>                 firstName: firstName,
>                 lastName: lastName,
>                 phoneNumber: phoneNumber,
>                 email: email
>             };
>     };

and another guys tells this one:

> function add(firstName, lastName, email, phoneNumber) {
>             contacts[contacts.length] = {
>                 this.firstName = "firstName";
>                 this.lastName = "lastName";
>                 this.phoneNumber = "phoneNumber";
>                 this.email = "email";
>             };
>     };

i've tried both, worked only first.

why do we have to use "this"?
why [equal sign = with "quotes"] and [just colon : with comma , at the end ]?


#2

is a string assignment. We need to assign the parameters, which are variables. That's why this won't work as expected.

Furthermore, we are not working with a context object, so this would not be used in this application, also why the second example doesn't work as expected.

The first example is the one to use.


#3

@alt_anton,

=== the Objects 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
a name property with property-key name and it's associated string VALUE "FIRST"

OR

var myObj = {
         type: 'fancy',
         disposition: 'sunny'
        }

myObj has 2 properties seperated by a comma-,,
a type property with property-key type and an associated string VALUE 'fancy'
a disposition-property with property-key disposition and
..an associated string VALUE 'sunny'.

= = = = = = = = = = = = = = = = = = = = = = =

To create an Object,

you can use the literal notation,

you directly create an Instance of the object, with the
properties being separated by a comma-,

  var myObj = {
        type: 'fancy',
        disposition: 'sunny' 
  };

OR

You create an Object by the construct notation.

First you create an empty Object by way of either
myObj = new Object(); or myObj = {};
and then you attach its properties using the syntax
object-name.property-key = it's-associated-value ;
( this.name = x ; )

thus:

  var myObj = {};
      myObj.type = 'fancy';
      myObj.disposition = 'sunny';

OR

There is also the facility Class construct notation.

The name should then start with a Capital-letter.

  var Person = function( theName, theAge ) {
         this.name = theName;
         this.age = theAge;
         this.displayInstance = function() {
           console.log("The displayInstance -output-"+
                       "\n============================" +
                     "\n\t name: " + this.name +
                     "\n\t age: " + this.age);
                       };
       };
    //now create an Instance of this Class-object
  var myObj = new Person("Classy_Rocker",20);
    //call the Method displayInstance which takes NO parameters
  myObj.displayInstance();
  console.log( myObj );
  //you can run this in labs.codecademy.com try it !!

As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.


6/6 why can't use "this" keyword?
#4

@alt_anton,

Option 1 Explained

You provided:

> function add(firstName, lastName, email, phoneNumber) {
>             contacts[contacts.length] = {
>                 firstName: firstName,
>                 lastName: lastName,
>                 phoneNumber: phoneNumber,
>                 email: email
>             };
>     };

Here you are adding a so-called literal-notated Object
to the Array contacts

Option 2 explained.

This will NOT function....

> function add(firstName, lastName, email, phoneNumber) {
>             contacts[contacts.length] = {
>                 this.firstName = "firstName";
>                 this.lastName = "lastName";
>                 this.phoneNumber = "phoneNumber";
>                 this.email = "email";
>             };
>     };

This will NOT function....

Option 3 as possibility

  • 1 You define a Object Class constructor Contact
  • 2 within the add-function you =add-a-contact= by creating a new-Instance of the Contact-Object-class-constructor

-

var contacts=[];

// the =Object Class constructor= Contacts
function Contact(firstName,lastName,number,email) {
    this.firstName= firstName;
    this.lastName=lastName;
    this.number=number;
    this.email=email;
}

// an =add= function
function add(firstName,lastName,number,email) {
    //
    console.log("== contacts-BEFORE ==");
    console.log( contacts );
    //
    // create a new-Contact-Instance AND insert into =contacts= Array
    contacts[contacts.length] = new Contact(firstName,lastName,number,email);
    //
    console.log("== contacts-AFTER ==");
    console.log( contacts );
    console.log("================================");
}
*/
add("JJ","Hutu","987-654321","ldm.@ldm.com");
add("JJ","Hutu","987-654321","ldm.@ldm.com");

6/6 We Made A Friend! Comprehension sake...adding two friends!
6/6 why can't use "this" keyword?
#5

Thanks a lot!!!
I think i got it now.
I learn programming language first time in my life, that's why i have many questions!


#6

To expand your knowledge

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
= http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
= http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1

Code-Year
https://www.codecademy.com/en/tracks/code-year?jump_to=4fce4211279bae0003006265

teach yourself javascript site:codecademy.com
https://www.codecademy.com/en/tracks/teachyourself