1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
var cu = require('class-utils');
// function App() {}
// Object.defineProperty(App.prototype, 'count', {
// get: function () {
// return Object.keys(this).length;
// }
// });
// console.log(cu.getDescriptor(App.prototype, 'count'));
function defineProp (obj, name, fn) {
Object.defineProperty(obj, name, {
enumerable: true,
configurable: true,
get: function () {
return fn();
}
});
}
function fn() {
console.log('hey!');
return function (msg) {
return 'foo ' + msg;
};
}
var one = {
bar: function (msg) {
return 'bar ' + msg;
}
};
var two = {};
defineProp(one, 'foo', fn);
cu.copyDescriptor(two, one, 'foo');
cu.copyDescriptor(two, one, 'bar');
console.log(two.foo('a'))
console.log(two.bar('b'))
|