除了从createApp的Object.assign劫持外
通过观察还可以发现use也是开发者常用的API
其源代码为
use(plugin, ...options) {
if (installedPlugins.has(plugin)) {
warn$1(`Plugin has already been applied to target app.`);
} else if (plugin && isFunction(plugin.install)) {
installedPlugins.add(plugin);
plugin.install(app, ...options);
} else if (isFunction(plugin)) {
installedPlugins.add(plugin);
plugin(app, ...options);
} else {
warn$1(
`A plugin must either be a function or an object with an "install" function.`
);
}
return app;
}
可以看到使用了installedPlugins
而该变量是一个WeakSet
const installedPlugins = /* @__PURE__ */ new WeakSet();
可知针对WeakSet进行劫持得到Plugin,对Plugin进行一层包装依然可以实现夺取appContext对象