These two days, not too many things, well, I admit, is that I do not want to do the company's project, because I whim, in fact, is not a whim, is the idea of the previous realization, is to write JS as java.
Now there are a lot of JS dynamic loading framework, such as In.js. But this is not the way I want to write, I say my thoughts.
First, a piece of Java code.
| The code is as follows |
Copy Code |
Import Biz.user;
User U = new user ();
U.show (); |
According to the process is to guide the package, instantiation, call.
JS is not able to do the guide package, or the code meaning of the guide package, usually just on the page for the introduction of the script tag.
So let's assume that you need to write
| The code is as follows |
Copy Code |
Using ("User");
var u = new User ();
U.show (); |
So, in JS can be implemented?
To a sentence of analysis, of course, the premise is that the page does not use a script tag loaded into the user.js, otherwise it is meaningless.
First sentence
using ("User"), why use, of course, just one of my naming ideas, you can think of C #, using the using, borrowed.
Using the is written in the object of course I need user, as the name implies, I certainly write using ("User"). Do not say how the inner is achieved, at least the idea is this.
Because the keyword could not be modeled as Using User; at least I can't do it.
The second and third sentences
| The code is as follows |
Copy Code |
var u = new User ();
U.show (); |
Very normal, is very common instantiation and function call, the only puzzled is the user object where come from? Then of course it was imported when the first guide package was introduced.
The process is such a process, then in the end can be achieved, the key is in the first sentence. That is to say, can guide the package successfully, and how to guide the package.
Draw inspiration from the script tag, right, and asynchronously load the required JS files.
Other words
| The code is as follows |
Copy Code |
| Using ("User"); |
The equivalent of writing a sentence
| The code is as follows |
Copy Code |
| <script type= "Text/javascript" src= "User.js" ></script> |
Does it make sense to look at it now? For the script tag written as JS dynamic introduction? Or, just write a few characters less?
Of course not, it makes no sense!
First, in terms of efficiency.
If a page needs to load more than n JS files, the following
| The code is as follows |
Copy Code |
<script type= "Text/javascript" src= "Jquery.min.js" ></script>
<script type= "Text/javascript" src= "View.js" ></script>
<script type= "Text/javascript" src= "Register.js" ></script>
<script type= "Text/javascript" src= "Validate.js" ></script>
<script type= "Text/javascript" src= "User.js" ></script>
<script type= "Text/javascript" src= "Order.js" ></script>
<script type= "Text/javascript" src= "Type.js" ></script> |
Wait, wait.
Isn't it scary, it's pretty scary, and late maintenance requires a lot of cost, and how many pages you might need to modify a few pages. So, when the page only introduces a few key JS files, other files are dynamically loaded in the way?
For example, we just need to load the jquery file and then call
| The code is as follows |
Copy Code |
| $.getscript ("User.js", function () {}); |
In this way, we just need to introduce the page file
| The code is as follows |
Copy Code |
| <script type= "Text/javascript" src= "Jquery.min.js" ></script> |
Can.
So what's the downside of this writing? Read a piece of code
| code is as follows |
copy code |
$.getscript (" User.js ", function () {
$.getscript (" Order.js ", function () {
$.getscript ("Type.js", function () {
$.getscript ("Validate.js", function () {
//And so on.
});
});
});
}); |
PS: Using the in.js watch function can prevent this from happening. This is beyond the scope of this blog post.
Do you have a mind? Would you like to align the code? Even if you have a formatting tool, would you like to match the closing bracket with which $.getscript? Of course I don't want to.
Then, The Imitation Java Guide package form is answered.
| The code is as follows |
Copy Code |
Using ("User");
Using ("Order");
Using ("Type");
Using ("Validate");
And so on. |
Or if you want, you can
| The code is as follows |
Copy Code |
| Using ("User", "Type", "Order", "Validate",...); |
The question of writing doesn't matter. Of course I recommend using the first method, clear.
After the guide package, all the usage does not need any nesting, normal use.
| The code is as follows |
Copy Code |
|
var u = new User ();
var o = New Order ();
And so on. But a question will be raised. If the asynchronous load is performed at the time of the using ("XXX"), then
Using ("User");
Using ("Order");
Using ("Type");
Using ("Validate");
And so on. |