Skip to content

Instantly share code, notes, and snippets.

@haruair
Last active August 29, 2015 14:02
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 haruair/a83d2f18d5619901bc4e to your computer and use it in GitHub Desktop.
Save haruair/a83d2f18d5619901bc4e to your computer and use it in GitHub Desktop.
defineProperty test
"use strict";
var Person = function(name){
var _name = name || "Unknown";
Object.defineProperty(this, "name", {
get: function(){ return "My name is " + _name; },
set: function(newvalue){ _name = newvalue; }
});
Object.defineProperty(this, "location", {
get: function(){ throw Error("NotImplementException"); },
configurable: true
});
};
Person.prototype.getBusinessCard = function(){
return "[Card] " + this.name + " " + this.location;
};
var Melbourner = function(){
Person.apply(this, arguments);
Object.defineProperty(this, "location", {
get: function(){ return "Melbourne" }
});
Object.freeze(this);
};
Melbourner.prototype = Object.create(Person.prototype);
var mindy = new Person('Mindy');
console.log(mindy.name);
try{
console.log(mindy.location);
} catch(e){
console.log(e);
}
var edward = new Melbourner('Edward');
console.log(edward.name, edward.location);
console.log(edward.getBusinessCard());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment