Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sinovic
Created January 8, 2018 15:23
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 sinovic/575cf4644f104faa763ec3a3e2a459f0 to your computer and use it in GitHub Desktop.
Save sinovic/575cf4644f104faa763ec3a3e2a459f0 to your computer and use it in GitHub Desktop.
Getters and setters - OOP Getters are convenient methods to get the value of specific properties; as the name suggests, setters are methods that set the value of a property. // source http://jsbin.com/ruwixuj
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Getters are convenient methods to get the value of specific properties; as the name suggests, setters are methods that set the value of a property.
">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Getters and setters - OOP</title>
</head>
<body>
<script id="jsbin-javascript">
var person = {
firstName: 'Albert',
lastName: 'Einstein'
};
Object.defineProperty(person, 'fullname', {
get: function() {
return this.firstName + ' ' + this.lastName;
},
set: function(name) {
var words = name.split(' ');
this.firstName = words[0];
this.lastName = words[1];
},
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">var person = {
firstName: 'Albert',
lastName: 'Einstein'
};
Object.defineProperty(person, 'fullname', {
get: function() {
return this.firstName + ' ' + this.lastName;
},
set: function(name) {
var words = name.split(' ');
this.firstName = words[0];
this.lastName = words[1];
},
});
</script></body>
</html>
var person = {
firstName: 'Albert',
lastName: 'Einstein'
};
Object.defineProperty(person, 'fullname', {
get: function() {
return this.firstName + ' ' + this.lastName;
},
set: function(name) {
var words = name.split(' ');
this.firstName = words[0];
this.lastName = words[1];
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment