Arrays and objects 14 heterogeneous arrays


#1

myArray = [1, false, "jason", cat.abc()]
var cat = {};

var cat.abc = function() { cat.name + cat.type}
cat.name = "gizmo"
cat.type = "wild"

Receiving error "unexpected token"

However, if I do this:

myArray = [1, false, "jason", cat]
var cat = {

name = 'Gizmo'
age = 3

};

I get error "unexpected identifier". What am I doing incorrectly and what is the proper method of including an object in an array using the 2 methods to call an object?


#2

Ok so this:

Javascript is an Async langauge. Meaning it goes from top to bottom. So at the top it dosent recognize wat cat is when you assign it to the myArray; What you can do instead is...

myArray = [1, false, "jason"];
var cat = {};

var cat.abc = function() { cat.name + cat.type}
cat.name = "gizmo"
cat.type = "wild"
myArray.push(cat.name);

Which pushes it to the array..

Hmm lets take a look at that code:

Notice something when you are assigning? This is the syntax:

var xyz = {
 x : 3,
 y : 4,
 5 : 6
};

#3

myArray = [1, false, "jason"];
var cat = {};

var cat.abc = function() { cat.name + cat.type}
cat.name = "gizmo"
cat.type = "wild"
myArray.push(cat.name);

Doesn' this ust push an element of the function to the array, how do you push the function itself?

I don't understand what you're doing with the second portion unless you're noting the missing comma?
Was that the only error in syntax that you were noticing?


#4

First portion:

Im going to explain push in a simple way.. (I hope you understand your problem about Async):

We have an array .

myArray = [1,2,3,4]

//Later i decide to let in 5 join the ride

myArray.push(5);
//So now my array is equal to [1,2,3,4,5]

Second Portion:

You can see more differences than just a comma....

My Code:

var xyz = {
 x : 3,
 y : 4,
 5 : 6
};

Your Code:


#5

If you push a value to an array does it persist? Across sessions?


#6

Yes sir. It presists as always..


#7

what does the ".abc" do in this code

myArray = [1, false, "jason"];
var cat = {};

var cat.abc = function() { cat.name + cat.type}
cat.name = "gizmo"
cat.type = "wild"
myArray.push(cat.name);


#8

var myArray = [1, false, "jason",

{
kind:"r",
tasty:"w",
ooo:11
}

]

/*My liitle try*/