Aarrrg - 6. We Made a New Friend! (Syntax Error: Invalid Shorthand...)


#1

https://www.codecademy.com/courses/building-an-address-book/0/6?curriculum_id=506324b3a7dffd00020bf661#


I really thought I was doing this right, but it's not working for me. The error message is "Syntax Error: Invalid Shorthand Property Initializer".

Can anyone help me? :frowning:


var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() {
	var contactsLength = contacts.length;
	for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

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

add("Ciji", "Prosser", "cprosser@example.com", "*69");
list();


#2

nodejs:

[stdin]:30
    firstName = firstName,
    ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer

firefox:

/*
Exception: SyntaxError: missing : after property id
@Scratchpad/1:30
*/

#3

replace = with : . for example firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber


#4

thank you!

can you also explain to me why this "add" function doesn't need "this." before each property?

i.e. why isn't the correct syntax:

this.lastName: lastName,
this.email: email,
etc.

This wasn't part of the mistake I made - I'm just curious.


#5

Okay as far as I can tell, in this case we have a function that takes these arguments like: function(firstName, lastName, email, phoneNumber)
so firstName and the like are all arguments that the function takes in. we cant use this.method here. Its for objects


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.