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?
aura:attribute
model in the child component please.