Prototype Property Descriptors copying
by imcrthy
JavaScript
console.clear();
function Person(name) {
// console.log('args', arguments);
//console.log('context', Object.keys(this));
//console.log('proper instance', this instanceof arguments.callee);
//console.log('run method', typeof this.run);
if (!(this instanceof arguments.callee)) {
//console.log('Snap! Unexpected context.');
for (var name in Person.prototype) {
if (!this.hasOwnProperty(name)) {
// console.log('binding prototype method', name, 'to', this.name);
var descriptor = Object.getOwnPropertyDescriptor(arguments.callee.prototype, name);
// console.log( descriptor );
Object.defineProperty(this, name, descriptor);
} else {
// console.log('NOT binding prototype method', name, 'to', this.name);
}
}
}
}
Object.defineProperty(Person.prototype, 'document', {
enumerable: true,
writable: false,
value: function document() {
console.log(this);
}
});
Person.prototype.run = function run() {
console.log('run');
}
Person.prototype.poop = function poop() {
console.log( arguments.callee.name );
console.log( typeof this.run );
//console.log( Object.keys( this ), this);
}
//var Andy = new Person( 'Andy' );
Person.call(window, 'Ivan');
//var bound = Test.bind({});
window.poop();