Home > Web Front-end > JS Tutorial > JavaScript namespace usage introduction_javascript skills

JavaScript namespace usage introduction_javascript skills

WBOY
Release: 2016-05-16 17:24:10
Original
1659 people have browsed it

Students who have used Java and C# are very familiar with namespaces. In a complex system, there will be many functions and objects, provided by the language and predefined by the architecture. With so many functions and objects, due to programming specifications If you ask for a name with practical meaning, it will inevitably lead to incorrect calls due to the same name. But with the namespace, the trouble is gone. Not only can functions and objects be classified and organized, but also isolation can be formed to solve the problem of duplicate names.

Using JavaScript is not so comfortable. Javascript only has function scope. Blocks and Shenma files are all considered to be a namespace. Sometimes errors caused by some duplicate names are baffling and difficult to understand. Debugging and solving.

A simple example

Copy code The code is as follows:



                                                                                                                                                                            

test2();
//.......
}

test2(){
function alert('test2')
                                


In this example, the performance will be different in different browsers. IE will report Stack over flow and Firefox will die. . . Anyway, it will report an error. It is a very simple error. An alert function is customized in the code. The test2 function is called in the alert function. The test2 function intends to call the alert method of the window. In this way, it is called in a loop. Maybe you will say it is so obvious after reading it. Who would make the mistake, but if the custom method is called close (this will often happen), and then an external file function is called internally, and the function calls the window's close method, the error will be hidden a lot.

Simple namespace

Since JavaScript does not have file scope, different functions are scattered in different files, or even written by different people, and the probability of duplicate names is greatly increased. Is it enough to be careful enough? Not necessarily. There are also some unexpected situations. For example, inheritance is often used, so I wrote a function name extend that has never appeared before. Unexpectedly, the extend function was added to EcmaScript5, and the necessity of namespaces was reflected.

JavaScript has function scope. You can use this to write custom functions into a function body, so that the variables, objects, and functions in the function are isolated from the outside just like they are in a namespace.


                                                                                                                    alert=function(){
console.log('test');
}
}




In this way, the customized alert method will not conflict with the window alert.
Simple evolution

This is possible, but there are problems. The biggest problem is that the calling method is complicated and ugly! The object must be instantiated every time it is called, and then its methods must be called. Simply modify the code to achieve automatic instantiation.

Copy code

The code is as follows:



           
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template