2

I would like to understand why the typeof a JavaScript variable is different depending on where I evaluate it, even though I have't changed it.

I have a object like this:

var model = {}
model.user = {}
model.caller = {}
model.caller.name = 'Batman';
model.other = {}
// etc

I pass it into a child Lightning component like this:

$A.createComponent(screen, {
        "aura:id" : "activeScreen",
        "model" : model
    }, function (contentComponent, status, error) {
        if (status === "SUCCESS") {
            target.set('v.body', [contentComponent]);
        } else if (status === "INCOMPLETE") {
            console.log("No response from server or client is offline.")
        } else if (status === "ERROR") {
            throw new Error(error);
        }
    });

It's stored in this attribute in the child component:

<aura:attribute name="model" type="Object" />

And in the child component get the model using this helper method:

model : function(cmp) {
    return cmp.get("v.model");
},

If I call this method and check the typeof the value return:

var model = helper.model(cmp);
console.log(typeof model);

I get an console out put of:

string

Whereas, if I evaluate the value within the helper method:

model : function(cmp) {

    var model = cmp.get("v.model");

    switch (typeof model){
        case 'string':
            console.log('string');  
            return JSON.parse(cmp.get("v.model"));

        case 'object':
            console.log('object');  
            return cmp.get("v.model");
    }

    return null;
},

The console out put is:

object

Why do I get a different type dependant on when I evaluate it?

  • Declaration of the aura:attribute model in the child component please. – N.. Jun 22 '18 at 10:24
  • @N.B. Added the attribute – Robs Jun 22 '18 at 10:26
  • 1
    Likely to be caused by LockerService proxying. See e.g. How Locker Service Uses the Proxy Object. One of these Proxy functions should give you the underlying object. – Keith C Jun 22 '18 at 10:27
  • @KeithC I imagined it might would be the LockerService... but I am still not sure why the LockerService is making it behaves like this in this case and what set of rules I can rely on. – Robs Jun 22 '18 at 10:32
  • The first link in my comment provides some of the rules. – Keith C Jun 22 '18 at 10:34
0

This is not an answer to:

Why do I get a different type dependant on when I evaluate it?

But if you are battling this issue, the below might help.


By setting the attribute type to string I consistently get an object

<aura:attribute name="model" type="String" />

Seem backwards... but it works... at the moment (June 2018)

|improve this answer|||||
  • 1
    Definitely don't do that. It may work today, but there's no guarantee it will work later. – sfdcfox Jun 22 '18 at 12:41
  • @sfdcfox Shall I delete this post then? – Robs Jun 22 '18 at 13:18
  • 1
    Were I you, I'd ask around on Twitter, Communities, or something, see if anyone knows what's going on. I wouldn't go so far as to delete this yet. I'm not sure I have an answer for you, but I'm pretty sure that using a String attribute and expecting an Object back out is not a good idea. – sfdcfox Jun 22 '18 at 13:22
  • @sfdcfox Thanks. I think setting an attribute to String and getting an Object is bonkers ;) – Robs Jun 22 '18 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.