Last active
December 21, 2015 22:09
-
-
Save redeyes2015/6373483 to your computer and use it in GitHub Desktop.
in response to http://www.phloxblog.in/javascript-oop-interesting-study/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript is a prototype-based language which contains no class statement | |
function makeAddressWith(streetno, streetaddress, city, state, country, pincode) { | |
return { | |
streetno: streetno , | |
streetaddress: streetaddress, | |
city: city, | |
state: state, | |
country: country, | |
pincode: pincode | |
} | |
} | |
function fullAddress(address) { | |
return 'Full Address is -->>' + '\n' + address.streetno + ' ' + | |
address.streetaddress + '\n' + address.city + ' ' + | |
address.pincode + '\n' + address.state + ' ' + address.country; | |
} | |
function addressWithCityAndPincode(address) { | |
return 'City and Pincode is -->>' + '\n' + address.city + ' ' + address.pincode ; | |
} | |
address = makeAddressWith("4C", "Chidam Mudi Lane", "Kolkata", "West Bengal", "India", "700006"); | |
console.log(fullAddress(address)); | |
console.log(addressWithCityAndPincode(address)); | |
// Now more concise definition of the class | |
function makeAnotherAddressWith(streetno, streetaddress, city, state, country, pincode) { | |
return { | |
streetno: streetno , | |
streetaddress: streetaddress, | |
city: city, | |
state: state, | |
country: country, | |
pincode: pincode, | |
fullAddress: function() { | |
return 'Full Address is (another function) -->>' + '\n' + address.streetno + ' ' + address.streetaddress + '\n' + address.city + ' ' + address.pincode + | |
'\n' + address.state + ' ' + address.country; | |
}, | |
addressWithCityAndPincode: function() { | |
return 'City and Pincode is (another function) -->>' + '\n' + address.city + ' ' + address.pincode ; | |
} | |
} | |
} | |
anotheraddress = makeAnotherAddressWith("5D", "Neque porro quisquam", "Dolorem", | |
"Adipisci Velit", "LoremIpsum", "600007"); | |
console.log(anotheraddress.fullAddress()); | |
console.log(anotheraddress.addressWithCityAndPincode()); | |
// Use `this` | |
function makeYetAnotherAddressWith(streetno, streetaddress, city, state, country, pincode) { | |
return { | |
streetno: streetno , | |
streetaddress: streetaddress, | |
city: city, | |
state: state, | |
country: country, | |
pincode: pincode, | |
fullAddress: function() { | |
return 'Full Address is (yet another function) -->>' + '\n' + this.streetno + ' ' + this.streetaddress + '\n' + this.city + ' ' + this.pincode + | |
'\n' + this.state + ' ' + this.country; | |
}, | |
addressWithCityAndPincode: function() { | |
return 'City and Pincode is (yet another function) -->>' + '\n' + this.city + ' ' + this.pincode ; | |
} | |
} | |
} | |
address3 = makeYetAnotherAddressWith("5D", "Neque porro quisquam", "Dolorem", | |
"Adipisci Velit", "LoremIpsum", "600007"); | |
console.log(address3.fullAddress()); | |
console.log(address3.addressWithCityAndPincode()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment