Skip to content

Instantly share code, notes, and snippets.

@redeyes2015
Last active December 21, 2015 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redeyes2015/6373483 to your computer and use it in GitHub Desktop.
Save redeyes2015/6373483 to your computer and use it in GitHub Desktop.
// 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