[英]How to load a class dynamically in Javascript ES6
In Javascript ES6, how do we load classes dynamically ? 在Javascript ES6中,我们如何动态加载类?
I'm building an asset manager and in the class 'asset' below there is a load()
method that uses the JQuery function $.getScript()
to load the file security.js . 我正在构建一个资产管理器,在下面的类“资产”中有一个
load()
方法,它使用JQuery函数$.getScript()
来加载文件security.js 。 Nonetheless the file security.js does load as I am able to see the Ajax call and the responses for it. 尽管如此,文件security.js确实加载,因为我能够看到Ajax调用及其响应。 However I am unable to initialize the class by,
但是我无法初始化类,
neither 也不
yellow = new asset_security();
nor 也不
let script = new window['asset_'+s['screen']]();
My Example: 我的例子:
class asset {
constructor() {
this.include = ['general','header','menu'];
this.css = [];
this.js = [];
// Create Defaults for the app
this.css['general'] = [];
this.js['general'] = [];
this.css['header'] = [];
this.js['header'] = [];
this.css['menu'] = [];
this.js['menu'] = [];
// this.css['general'][this.css['general'].length] = 'initial.css';
// this.js['general'][this.js['general'].length] = 'controller/initial.js';
// this.css['header'][this.css['header'].length] = 'header/header.css';
// this.js['header'][this.js['header'].length] = 'header/controller/header.js';
// this.css['menu'][this.css['menu'].length] = 'menu/header.css';
// this.js['menu'][this.js['menu'].length] = 'menu/controller/menu.js';
}
load() {
let s = new status(0);
/**
* @param data ~ Content of the script returned
* @param textStatus ~ Verbal response to success or fail
* @param browser ~ browser.status returns the 200/404/500 statuses
*/
$.getScript( "/js/asset/"+s['screen']+'.js', function( data, textStatus, browser ) {
var exist = false;
try {
exist = (typeof 'asset_'+s['screen'] === 'function');
} catch (e) {}
if ( exist ) {
//TODO: figure out how to load class dynamically
/*
let script = new window['asset_'+s['screen']]();
script.load();
script.render();
*/
}
});
}
The response that I get from the console state :ReferenceError: asset_security is not defined
我从控制台
state :ReferenceError: asset_security is not defined
得到的响应state :ReferenceError: asset_security is not defined
What is the proper way to initialize the class after $.getScript()
run ? $.getScript()
运行后初始化类的正确方法是什么?
This is how the asset_security class looks like: 这就是asset_security类的样子:
class asset_security extends asset {
constructor() {
super();
this.className = 'asset_security';
this.include = ['security'];
this.css['security'] = [];
this.js['security'] = [];
}
load() {
// this.css['security'][this.css['security'].length] = 'security/formLogin.css';
// this.js['security'][this.js['security'].length] = 'security/controller/security.js';
// this.js['security'][this.js['security'].length] = 'security/modal/screen.js';
// this.js['security'][this.js['security'].length] = 'security/view/screen.js';
}
}
I see that you're trying to load a JavaScript file after downloading it from some external resource. 我看到你试图从一些外部资源下载后加载一个JavaScript文件。 You can create a script tag and load your JavaScript class file through it.
您可以创建脚本标记并通过它加载JavaScript类文件。
Example: 例:
if ( file_exists ) {
var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.