Do you think you know Javascript very well ?, Javascript?

Source: Internet
Author: User

Do you think you know Javascript very well ?, Javascript?

(Sorry for your improper translation)

Source: http://www.ido321.com/914.html

There are five small scripts to help you really understand the JavaScript core-closure and scope. Before running on the console, try to answer what will pop up in each case. Then you can create a test file to check your answer. Are you ready?

1,

   1: if (!("a" in window)) {
   2:     var a = 1;
   3: }
   4: alert(a);

2,

   1: var a = 1,
   2:     b = function a(x) {
   3:         x && a(--x);
   4:     };
   5: alert(a);

3,

   1: function a(x) {
   2:     return x * 2;
   3: }
   4: var a;
   5: alert(a);

4,

   1: function b(x, y, a) {
   2:     arguments[2] = 10;
   3:     alert(a);
   4: }
   5: b(1, 2, 3);

5,

   1: function a() {
   2:     alert(this);
   3: }
   4: a.call(null);

My predictions are: undefined, 1, unknown, 10, null

The answer is at the end of this Article. before reading the answer, do you dare to leave your guesses?

 

 

 

 

Correct answer: 1. undefined 2, 1 3, function a (x) {return x * 2} 4, 10 5, [object window]

Source: http://dmitry.baranovskiy.com/post/91403200


I thought I knew the scope in javascript very well, but it didn't seem like this ...... Solution

The landlord first declared a global variable msg, and wrote another var msg = "hello" in the method ";
This statement does not change the value of the global variable msg,
Because the msg scope in setTimeout ("alert (msg);", 2000) is global, the printed value is also global,
If you change var msg = "hello"; to msg = "hello ";
Then the printed global variable value will change to your desired hello

Why is your Javascript technology so bad?

Its low learning threshold makes many people refer to it as a pre-school scripting language. Another thing that makes people laugh at is the concept of Dynamic Language, which uses a high standard of static data. In fact, you and Javascript both stand wrong, and now, you make Javascript very angry. There are five reasons to explain that your Javascript technology is poor. 1. You are not using a namespace. Do you still remember that the university teacher told you not to use global variables in your homework? The use of global variables in Javascript is no exception. Web pages become messy and messy, and scripts and script libraries are everywhere from every corner of the Internet. If you name a variable loader (), you are looking for trouble. Javascript will not remind you If you reload a function unconsciously. You still call it a preschool education programming language, remember? What I want to say is that you need to know what will happen after doing this. Function ?? Derp () {alert ("one");} function ?? Derp () {alert ("two") ;}derp (); "two", The answer is "two ". This is not always the case. It may also be "one ". Therefore, it is easy to put all your code in your namespace. The following is a simple way to define your own namespace. Var foospace ={}; foospace. derp = function () {alert ("one") ;}function ?? Derp () {alert ("two") ;}foospace. derp (); 2. You are performing a magic trick. You define the variable one East and one west. Using a combination of numbers, letters, and numbers as variable names is a double-win conclusion. Looking for a variable without any ideographic characters in a code block of 40 lines is a nightmare for maintenance. Mixing the first declaration of variables into a 40-line code block is also a nightmare. Even if you encounter such a variable, you may not ask yourself: "Where is this defined ?", Then, we quickly use Ctrl + F to find the initial position of the variable in the source code. No, don't. On the contrary, this is an abuse of Javascript, and it is a stupid way. You should always define the variable at the top of its scope of use. It cannot be said that because it is not necessary, you can not do so. Function () {var ,?? // Descriptionb ;?? // Description // process ...} 3. You do not understand the scope of Javascript variables. You are a talented programmer. You eat C ++ and pull a List. You know what the range of variables is. You have full control over your variables, and you are watching them like they are too powerful. However, Javascript makes a bubble in your coffee and laughs. Var herp = "one"; {var herp = "two";} alert (herp); in this case, the herp you get is not "one", but "two ". The effective scope of Javascript variables is not dependent on code blocks as in other languages. The Javascript variable range is based on the function. Every function has its own variable range. Javascript is so cool that it ignores the meaningless Scope of the arms. In fact, Javascript is so cool that you can even pass variable ranges like namespaces or variables. 4. You think Javascript's object-oriented features are only grafted. Javascript has been an object-oriented language since its launch. Everything is an object in Javascript, and everything! Even text symbols such as numbers and characters can be converted into objects through its own constructor. Compared with other object-oriented languages, Javascript differs in the following:

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.