In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "example Analysis of imitating C # coding in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The first is the bottom part of the JavaScript code:
Var jmz = {}; / / the storage object of all code jmz.demo = {}; / / an demo storage object is extended under jmz
/ / the above two are similar to NameSpace in .NET, and the following method programming also imitates the coding method of C #.
Jmz.o = {}; / / is used to store all global variables
There are three things defined here, and all the static, methods and variables are derived from under jmz.
The benefit of ◆: no matter how many lines of code you write, it all takes up only one global variable, so that you don't have any variable name conflicts when you work with other programmers.
The downside of ◆: if you write it later, the calling code will be too long, but there is also a way to handle it. Here is a hint for with.
Jmz.o is used to store the global variables you want to add to the web page. Of course, you are free to expand and optimize. If you have any good ideas, I hope you can contact me to discuss them.
Next are two typical examples, a class that simulates C # static methods and a class that simulates C #.
Static method:
Jmz.demo.HelloWorldA = function (str) {/ / simulate the static method invocation mode of C # alert (str);}
This is similar to the original JS function, and the calling mode is also similar to the static calling mode of C #. It can be used in some very common methods, such as getting the object, getting the string length, adding events to the object, and so on.
Class writing:
Jmz.demo.HelloWorldB = function () {var _ data = ""; / / Global private variables in class, prefixed with "_" to distinguish private methods in function Process (str) {/ / class. Private methods are written in uppercase if (str.length > 0) _ data = str; else return false; return true. } function Show () {alert (_ data);} this.process = public method in class function (str) {/ / for external calls, initials return Process (str);} this.show = function () {Show ();}}
This kind of writing feels good to me, and I define the code specification for myself to distinguish between private and public methods, private variables and common variables.
All right, the above code framework has been written, so let's make an actual call.
Jmz.Bind ("load", function () {jmz.demo.HelloWorldA ("Chinajmz:HelloWorldA!"); / / static method call jmz.o.helloWorldB = new jmz.demo.HelloWorldB (); / / initialize the class and store it in the object of jmz.o if (jmz.o.helloWorldB.process ("Chinajmz:HelloWorldB!") = = true) {/ / Public method call jmz.o.helloWorldB.show ();}})
I used my previous JS event binding method here. If you are interested, you can check it out.
Effect:
This is the end of the content of "example Analysis of imitating C # coding in JavaScript". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.