python-archieve-projects/WechatBot/node_modules/clone-class/dist/cjs/examples/example.js

42 lines
1.5 KiB
JavaScript
Executable File

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// eslint disable lowerCamelCase
const assert_1 = __importDefault(require("assert"));
const mod_1 = require("../src/mod");
class Employee {
name;
static company;
constructor(name) {
this.name = name;
}
info() {
console.info(`Employee ${this.name}, Company ${this.constructor.company}`);
}
}
console.info(`
# Example 1: cloneClass()
`);
const GoogleEmployee = (0, mod_1.cloneClass)(Employee);
GoogleEmployee.company = 'Google';
const MicrosoftEmployee = (0, mod_1.cloneClass)(Employee);
MicrosoftEmployee.company = 'Microsoft';
const employeeGg = new GoogleEmployee('Tom');
const employeeMs = new MicrosoftEmployee('Jerry');
employeeGg.info();
// Output: Employee Tom, Company Google
employeeMs.info();
// Output: Employee Jerry, Company Microsoft
console.info(`
# Example 2: instanceToClass()
`);
const RestoreGoogleEmployee = (0, mod_1.instanceToClass)(employeeGg, Employee);
(0, assert_1.default)(RestoreGoogleEmployee === GoogleEmployee, 'Should get back the Class which instanciated the instance');
(0, assert_1.default)(RestoreGoogleEmployee !== Employee, 'Should be different with the parent Class');
const anotherEmployee = new RestoreGoogleEmployee('Mary');
anotherEmployee.info();
// Output: Employee Mary, Company Google
console.info();
//# sourceMappingURL=example.js.map