I'm tried to implement objects extension in JavaScript but I don't understand why this code does not work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Question 027</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button onclick="retrievingAddressData()">getting address data</button> <br />
<p id="objectData">Object Data here</p>
<script>
const Customer = function (firstName, lastName,address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
function Address() {
return this.address;
}
}
Customer.prototype = {
parseAddress: function (data) {
//this.address = data.address;
this.address = data;
}
};
Customer.prototype.loadAddress = function () {
var customer = this;
$.get('data.xml', function (data) {
//customer.parseAddress(data);
customer.parseAddress($(data).find('address').text());
})
.done(function (data) {
//customer.parseAddress($(data).find('address').text());
console.log('all rigth!');
})
.fail(function () { console.log('error:') })
.always(function () { console.log('finished') });
}
//
function retrievingAddressData() {
const objCustomer = new Customer("Luz", "Tellez","Madrid");
var respuesta = document.getElementById("objectData");
respuesta.innerHTML = "*** Customer:firstname: "
+ objCustomer.firstName + " lastname:" + objCustomer.lastName;
objCustomer.loadAddress();
console.log(objCustomer.Address);
respuesta.innerHTML += " address:" + objCustomer.Address;
}
</script>
</body>
</html>
The content of xml file is summarize here: data.xml
<Customer>
<address>Madrid</address>
</Customer>
When I recover Xml data and update the object with extended prototype method, the value is not udpated, and when I recover the address value at the end of my code, this is: 'undefine'
Why?