made with esnextbin
Created
May 7, 2016 07:04
-
-
Save mauriciopoppe/fbef6fa9c9cb9739675f86f6f7c92f9c to your computer and use it in GitHub Desktop.
esnextbin sketch
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 charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</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
function freezeInstance(T) { | |
return new Proxy(T, { | |
construct: function (target, argumentLists, newTarget) { | |
var instance = new target(...argumentLists) | |
Object.freeze(instance) | |
return instance | |
} | |
}) | |
} | |
class A { | |
constructor() { | |
this.name = 'a' | |
} | |
getName() { | |
return this.name | |
} | |
} | |
A = freezeInstance(A) | |
class B extends A { | |
constructor() { | |
super() | |
this.name = 'b' | |
} | |
} | |
B = freezeInstance(B) | |
var a = new A() | |
console.log(a.getName()) | |
a.name = 'b' | |
console.log(a.getName()) | |
var b = new B() | |
console.log(b.getName()) | |
b.name = 'a' | |
console.log(b.getName()) |
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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"babel-runtime": "6.6.1" | |
} | |
} |
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 _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | |
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | |
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | |
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | |
var _inherits2 = require('babel-runtime/helpers/inherits'); | |
var _inherits3 = _interopRequireDefault(_inherits2); | |
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
var _createClass2 = require('babel-runtime/helpers/createClass'); | |
var _createClass3 = _interopRequireDefault(_createClass2); | |
var _freeze = require('babel-runtime/core-js/object/freeze'); | |
var _freeze2 = _interopRequireDefault(_freeze); | |
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | |
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
function freezeInstance(T) { | |
return new Proxy(T, { | |
construct: function construct(target, argumentLists, newTarget) { | |
var instance = new (Function.prototype.bind.apply(target, [null].concat((0, _toConsumableArray3.default)(argumentLists))))(); | |
(0, _freeze2.default)(instance); | |
return instance; | |
} | |
}); | |
} | |
var A = (function () { | |
function A() { | |
(0, _classCallCheck3.default)(this, A); | |
this.name = 'a'; | |
} | |
(0, _createClass3.default)(A, [{ | |
key: 'getName', | |
value: function getName() { | |
return this.name; | |
} | |
}]); | |
return A; | |
})(); | |
A = freezeInstance(A); | |
var B = (function (_A) { | |
(0, _inherits3.default)(B, _A); | |
function B() { | |
(0, _classCallCheck3.default)(this, B); | |
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(B).call(this)); | |
_this.name = 'b'; | |
return _this; | |
} | |
return B; | |
})(A); | |
B = freezeInstance(B); | |
var a = new A(); | |
console.log(a.getName()); | |
a.name = 'b'; | |
console.log(a.getName()); | |
var b = new B(); | |
console.log(b.getName()); | |
b.name = 'a'; | |
console.log(b.getName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment