I am fairly new to programming and have come across this problem when exploring objects. It appears that an if-statement can search for a property inside of an object without the use of hasOwnProperty() or the keyword 'in' or anything else for that matter. You can just type something like:
if (object.property) {code here};
I searched the web but no content on why this is possible. Except some shallow discussions on truthy or falsy concepts. Which almost helped but were to shallow. Below is the sample code I used to discover this problem
var obj = {
prop1: {
propa: "x",
propb: ["b", "q", "y"],
prop3: "y",
prop4: "zz"
}
},
output = document.getElementById("output");
if (obj.prop1) {
output.innerHTML = "The propery exist."; //The property exist shows.
} else {
output.innerHTML = "The property does not exist.";
}
So the question is What is the process that a Javascript if-statement performs to return a boolean answer from a statement such as if (object.property) {}?
Aucun commentaire:
Enregistrer un commentaire