After this sentence is executed, u is a variable with no actual meaning, nothing more. So, how to solve this problem, the only way I can think of for the time being is to use a synchronization strategy. Only when the js is loaded, the subsequent js statements are executed. This is a bit regretful, and the possible browser suspension caused by synchronization is also a serious problem. I will ignore these problems for the time being and hope that there will be a better solution in the future. .
Then the question arises, are there any advantages to synchronizing like this?
I don’t know what the advantages are. At least compared to asynchronous loading, there should be no disadvantages. For example, normal asynchronous loading is
$.getScript("user .js",function(){
var u = new User();
});
By simply executing this statement, to execute the function, you are essentially waiting for the user. js will not be executed until it is loaded, then compare
var u = new User();
Theoretically the time should be the same, because they are all executed after user.js is loaded.
At least the second one looks more like Java-style code, so don’t bother with other non-business-related codes.
So, how do you know where the required object is and how to load it? All I can think of is simulating a configuration file. Why use a configuration file instead of using the add function like In.js or the register-like function of other frameworks? Maybe I just want to use the configuration file, which is more like Java, and can be modified later. It will also be more decoupled.
Using.Config = {
"User" : "/js/user" // You can hide .js because the JS file must be loaded
}
The whole idea is roughly like this, and I have made some constraints on it. For example, after adding the namespace
var u = new Using.Modules .User();
This can reduce some global variables, and if necessary, you can insert some commonalities that all objects may have, reducing repeated coding when creating classes.
Of course, not using namespaces is still supported.
In order to solve the effectiveness of this constraint, the Class.create function is added to create class constraints.
Using.Class.create("User",function( ){
}).property({
}).static({
}).namespace(Using.Modules);
The general meaning here is
create (class name, constructor)
property (property of the class)
static (static property of the class)
namespace (namespace)
Extensions to this, why not Add MVC form?
Later I found out that if I want MVC, it requires dynamic maintenance between several classes, or automatic maintenance by the Using class when it is created. I haven’t thought of a good solution yet, so I haven’t joined it. , you can only create the class yourself and maintain it yourself.
Through the above text, you finally get a Using.js
, and then you only need to introduce a
Then you can write
Using("jquery");
Using("User");
$("#ID").click(function(){
var user = new User ();
user.name = "xx";
user.show();
});