Skip to content Skip to sidebar Skip to footer

I Cannot Access Fields In My Object Directly Or With A Getter Function

I've been banging my head against the wall on this trivial problem for hours. I'm simply trying to do what the code below shows and it doesn't work for me. It works fine in JSFidd

Solution 1:

The splice function returns an array containing the removed elements. If only one element is removed, an array of one element is returned. (source)


This means that this code adds an array containing the object to testArray, rather than just adding the object:

testArray.push(availableCardsForSet.splice(Math.floor(Math.random()*availableCardsForSet.length),1));

It can easily be fixed using [0]

testArray.push(availableCardsForSet.splice(Math.floor(Math.random()*availableCardsForSet.length),1)[0]);

Here's an example: http://jsfiddle.net/grc4/8Auvm/

Solution 2:

I tried it with below doctype it is working for me,

browsers tested: IE8,Firefox3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

function Card (first , last , mid) { this.first = first ; this.last = last ; this.mid = mid ; } var testArray = [ new Card("A", "B", "C"), new Card("D", "E", "F") ];alert(testArray[1].mid);

Post a Comment for "I Cannot Access Fields In My Object Directly Or With A Getter Function"