confusing assignment issue where a == a, returns false?

27 views
Skip to first unread message

James Russo

unread,
May 26, 2013, 1:21:52 AM5/26/13
to casp...@googlegroups.com
Hello, 

I'm confused as to why the following casper code would both produce a false result...

    objDocument = eobDocuments[0];
    this.echo(objDocument == eobDocuments[0]); // prints false
    this.echo(eobDocuments[0] == eobDocuments[0]); // prints false

The eobDocuments is a global variable (var eobDocuments at start of script) and is produced by the following function: 

function findLinks() {
    var map;
    map = Array.prototype.map;
    return map.call(document.querySelectorAll('tr.app_list_row1, tr.app_list_row2'), function(row)
            {
                columns = row.querySelectorAll('td');
                url = columns[0].querySelectorAll('a')[0].href;
                date = columns[0].querySelectorAll('a')[0].innerText.replace(/\//g,".");
                type = columns[1].innerText;
                language = columns[2].innerText;
                return {date: date, type: type,language: language, page_url: url, pdf_url: null };
            });
}

That function is called like this (after loading the correct document)... 

casper.then(function() { eobDocuments = this.evaluate(findLinks); });

Any help would be appreciated. Maybe I'm just not understanding some javascript fundamentals here or something..

Thanks,

-jr

Nicolas Perriault

unread,
May 26, 2013, 2:59:32 AM5/26/13
to casp...@googlegroups.com
On Sun, May 26, 2013 at 7:21 AM, James Russo <j...@halo3.net> wrote:

> Any help would be appreciated. Maybe I'm just not understanding some
> javascript fundamentals here or something..

Please provide a minimal but complete reproducible test case
(something I can run) so we can investigate

++

--
Nicolas Perriault
https://nicolas.perriault.net/
Phone: +33 (0) 660 92 08 67

James Russo

unread,
May 26, 2013, 7:43:55 AM5/26/13
to casp...@googlegroups.com, nic...@perriault.net
Hello Nicolas, 

Thanks. Sorry I didn't include it earlier. Was late here, and had been beating my head against the wall on this for about 3 hours.. One of those issues where you begin to question everything you know about a language.. 

Here is a complete test case, which I would expect to completely pass.

testcase.html file:

<html>
<body>
    <ol>
        <li>1
        <li>2
        <li>3
        <li>4
    </ol>
</body>
</html>


testcase.js:

var eobDocuments;
var utils = require('utils');
var casper = require('casper').create({
    verbose: true,
    viewport: {width: 1280, height: 1024 },
    logLevel: "debug"
});

function findLinks() { 
    var map;
    map = Array.prototype.map;
    return map.call(document.querySelectorAll('li'), function(row) 
            { 
                return { 'key': row.innerText, 'key2': 'value2' };
            });
}

casper.start('testcase.html');

casper.then(function() { 
    eobDocuments = this.evaluate(findLinks);
});

casper.then(function() { 
    this.capture('output.png');
});

casper.run(function() { 
    utils.dump(eobDocuments);
    eobDocument = eobDocuments[0];
    testLocal = [ { 'a': 1, 'b': 2 }, {'a': 3, 'b':4 }];
    testLocal0 = testLocal[0];
    this.test.assertTrue(testLocal0 == testLocal[0]); // This passes. 
    this.test.assertTrue(eobDocument == eobDocuments[0]); // Why should't this pass? 
    this.test.assertTrue(eobDocuments[0] == eobDocuments[0]); // same as this,should be same as 
    this.test.renderResults(true);
    this.exit();
});

Nicolas Perriault

unread,
May 26, 2013, 8:46:17 AM5/26/13
to casp...@googlegroups.com
On Sun, May 26, 2013 at 1:43 PM, James Russo <j...@halo3.net> wrote:

> Here is a complete test case, which I would expect to completely pass.

Seems like you're messing up with object identities; here's a passing
test suite using latest master:
https://gist.github.com/n1k0/5652689

Hope it helps

James Russo

unread,
May 26, 2013, 9:20:42 AM5/26/13
to casp...@googlegroups.com
Thanks! My underlying issue is trying to pass an object from the array into another run method, modify that object, and have that chane be reflected in the element in the array.

Something like this:

a = arr[0];
a.foo = '123';

I would expect arr[0].foo at this point to be '123', but I am not seeing that. I will work up a better test case when I get back to real computer.

-jr


--
Sent from my iPhone. Please pardon any typos.
> --
> CasperJS homepage & documentation: http://casperjs.org/
> CasperJS @github: https://github.com/n1k0/casperjs
>
> You received this message because you are subscribed to the Google Groups "casperjs" group.
> Visit this group at http://groups.google.com/group/casperjs?hl=en.
> ---
> You received this message because you are subscribed to the Google Groups "CasperJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to casperjs+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

James Russo

unread,
May 26, 2013, 9:39:42 PM5/26/13
to casp...@googlegroups.com
Here is a better test case, which explains my issue... 


So, when when assigning eobDocument = eobDocuments[0], and then setting a property on eobDocument is it not reflected in eobDocuments[0] array. Almost like the assignment of eobDocument = eobDocuments[0] is making a copy.. 

thanks,

-jr

Nicolas Perriault

unread,
May 27, 2013, 3:22:03 AM5/27/13
to casp...@googlegroups.com
On Mon, May 27, 2013 at 3:39 AM, James Russo <j...@halo3.net> wrote:

> So, when when assigning eobDocument = eobDocuments[0], and then setting a
> property on eobDocument is it not reflected in eobDocuments[0] array. Almost
> like the assignment of eobDocument = eobDocuments[0] is making a copy..

fixed https://github.com/n1k0/casperjs/commit/56dce2253f01775fda33af14ef742048289a2ada

James Russo

unread,
May 27, 2013, 8:02:51 AM5/27/13
to casp...@googlegroups.com
Thank you sir!

--
Sent from my iPhone. Please pardon any typos.

Reply all
Reply to author
Forward
0 new messages