Evasion of JavaScript multi-human development functions and variable names

Source: Internet
Author: User

The name of the function and the variable is always a headache, the first variable bar, I believe that know JS friends know that in JS

There is no block-level scope for only function scopes, which means that blocks of code that are delimited by curly braces cannot be defined

The scope of the variable, for example:

1 {2     var num = 110;3}4 console.log (num);//print Result: 110 instead of NUM is not defined
1 for (var j = 0; J < 5; j + +); 2 Console.log (j);/results: 5

How to solve? In general, experienced programmers will minimize the use of global variables and use local variables whenever possible, which will not only reduce the chance of variable names.

It also reduces memory overhead because local variables are typically automatically destroyed after the function ends, and global variables are not destroyed until the end of the process

Off. Second, when we need a scope to close the variable, we typically use an anonymous function to act as the scope. Such as:

1 (function () {2     var gender = ' Male '; 3}); 4 console.log (gender);//Result: Gender is not defined

Anonymous functions act as scopes This method is generally able to meet the needs of some programmers, but the question is, how to solve the function name?

How many global variables must be used in a project and how should they be resolved?

In fact, the solution is the same as the above method of thinking, just a little bit of skill, JS since the lack of scope limitations, then we will give it to it artificially plus a

Scope, and ensure that each scope is not duplicated, so that the problem is not resolved. Okay, so the next thing we're missing is a magical scope.

Alternative, happily, this thing itself JS there, that is the object!

Specifically we can do this: if there are now three students to work together an educational system project, the system needs teachers and students and staff

The information, three students division of Labor to solve these three kinds of information work, the specific code is as follows:

1//A students responsible for staff information 2 var a = {}//define an empty object 3 A.name = ' Tom '; 4 A.gender = ' Male '; 5 A.age = 30; 6 a.showname = function () {7     alert (this.name), 8} 9 A.showage = function () {Ten     alert (this.age), one}12//b classmate Negative Responsible Teacher Information var B = {}15 b.name = ' Jack '; b.gender = ' male '; b.age = 28;18 b.showname = function () {     alert (This.nam e)}21 b.showage = function () {     alert (this.age);}24//C students are responsible for student information of var C = {}27 c.name = ' Lucy '; c.gende r = ' female '; c.age = 17;30 c.showname = function () {(     this.name);}33 c.showage = function () {     alert (this.age); 35}

You can see that the module in charge of the ABC classmate has the same name area, Name,age,showname,showage and so on, but this time does not

Conflicting issues, because these variables and methods are mounted on different objects, which virtually adds a single variable and method to each of the same names.

The top namespace, so the problem that bothers us can be solved easily.

1//normal use, no conflict 2 console.log (a.name);//Results: Tom3 console.log (b.name);//Results: Jack4 console.log (c.name);//Result: Lucy5 A.showage ();//Results: 306 b.showage ();//Results: 287 c.showage ();/results: 17

The most effective method, or the use of object-oriented development bar, object-oriented is very suitable for team development, so as to maximize the risk of reducing code coupling, the project is also easy to maintain.

Evasion of JavaScript multi-human development functions and variable names

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.