Last active
August 29, 2015 14:02
-
-
Save haruair/a83d2f18d5619901bc4e to your computer and use it in GitHub Desktop.
defineProperty test
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
"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