程序是gtk的扩展插件 解析器是gjs 但是原理应该差不多 简化的逻辑如下
// 模拟类对象
function Class(params) {
for (let param in params) {
this.__proto__[param] = params[param];
}
let that = this;
return function () {
this.__proto__ = that.__proto__;
}
}
var Person = new Class({
name: 'anonymous',
say: function (name) {
if (name) {
console.log(`my name is ${name}`)
this.name = name;
} else {
console.log(`my name is ${this.name}`)
}
// console.log(this); // this的打印结果为Window
// Person.work(); // this.work is not a function;
this.work(); // this.work is not a function;
},
work: function () {
console.log(`${this.name}: do my work`)
}
});
// 某公共库方法 内容未知 且不可修改 此处仅为简单实现
function test(func) {
let name = '张三';
if (func) {
try {
func(name);
} catch (TypeError) {
console.error(TypeError.message)
}
}
}
var p1 = new Person();
test(p1.say);
console.log('--------------------------------------')
var p2 = new Person();
p2.say("猜猜我是谁");
模拟类的实现是因为源码是new lang.Class(); 也就是gjs是模拟了Class的实现 这里我也只做一个模拟
test是一个公共库的方法 具体实现未知 公共库就算看了源码也不能改 这里也只是简单模拟
调用person的方法say的时候会调用this.work
提示this.work不是一个方法 打印出来的是undefined
这块有没有啥方法能改