Created
January 8, 2018 15:23
-
-
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
This file contains hidden or 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
<!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> |
This file contains hidden or 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
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