JavaScript God-like variable system

Source: Internet
Author: User
Tags object object

In a word, the mystery of the story of JavaScript is presented. Crossing you probably understand that when JavaScript was born, it was never entrusted to the task. Brandon-Ike does not anticipate that today's JavaScript becomes so important. Otherwise, he would not be so reluctant to ink, unexpectedly only engaged in a "Var" declaration on the Sea of hundred rivers. Of course not to say so cherish ink has what major flaw, others inside make a type inference that is also cow b roar. In other words, Microsoft's C # also has a magical Var, plagiarism of Brandon-ike idea suspect!

Brandon-Ike so cherish ink, make a var even, Even our back-end shoes are the most familiar data types. Back-end shoes with a look at this JavaScript, how even the statement is simply a Var, our familiar friends "int, float, double, char, long ..." also disappeared. The students were hard to accept, and at first it was a bit of contempt for JavaScript, which seemed to lack arms and legs. As a matter of all, the students are dismissive, the script on the page is written out of the imagination, anyway, not the error on the line.

  This floor of a nonsense, do not know whether it is the voice of crossing. Yes, the JavaScript variable system was simplified by the Brandon-Ike guy, professionally called the "weak-type speech system." This play is good or bad is not conclusive, when originally did not intend to let JavaScript do what big job, simplifying design is also created by the Times. However, shoes with the same people want to play JavaScript in hand, those if...else, do....while you can despise, but its variable type system still have to take a look! This is also the building of this blog to learn together.

  Good! Our title is called "JavaScript God-like variable system", let's see how God Law!

Variable type of few but good

  Shoes with all should have a job experience, the landlord was such a question was asked down. "You say you JS write also can, then ask JS inside what data type?" ”。 Instantaneous misfiring, of course you can say JS that simple words, do not need to know what data type, can also write full screen swirling. But the details determine success or failure, the landlord can not say, the offer may also fly. What are the data types of JavaScript, in a nutshell:

  The numeric type has only one (number), the string is not true (Boolean) that must be kept, null (NULL) plus undefined (undefined) is also commonly used, arrays (array) that must not run, there is a God object, Plus a headache function.

  Yes, that is the above sentence, maybe you can get an offer. But the above sentence still has to compress to say, what meaning? It means that the above words are unfolded, so why not say the compressed first, and then start again? That is the landlord I want to let you remember a bit more deeply. To be exact, the types mentioned above are the exact types we use for coding, but for JavaScript type system types, the actual types are as follows:

  The God object contains null, an object (such as Var obj={...}), and a array;number type that includes all values of all numeric types; Boolean, string that is almost any word is not missing; function,undefined is JavaScript specific, and see the following code:

  

        var myvar=null;        Console.log ("MyVar is" +myvar+ "typeof=" +typeof (MyVar));//Result: MyVar is null typeof=object var myvar5={name: "K"}; Console.log ("MyVar5 is" +myvar5+ "typeof=" +typeof (MYVAR5));//Result: MYVAR5 is [Object Object] Typeof=object VA        R Myvar6=[1,2,3,100];        Console.log ("MYVAR6 is" +myvar6+ "typeof=" +typeof (MYVAR6));//Result: MYVAR6 is 1,2,3,100 typeof=object var myVar1; Console.log ("MyVar1 is" +myvar1+ "typeof=" +typeof (MYVAR1));//Result: MyVar1 is undefined typeof=undefined var MyV        Ar2= "Kevin"; Console.log ("MyVar2 is" +myvar2+ "typeof=" +typeof (MYVAR2));//Result: MYVAR2 is Kevin typeof=string var myvar3=100.0000        00001; Console.log ("MyVar3 is" +myvar3+ "typeof=" +typeof (MYVAR3));//Result: MyVar3 is 100.000000001 typeof=number var myvar4=        True Console.log ("MYVAR4 is" +myvar4+ "typeof=" +typeof (MYVAR4));//Result: MYVAR4 is true typeof=boolean var myvar7=function () {} console.log ("MyVar7 is" +myvar7+ "typeof=" +typeof (MYVAR7));//myvar7 is function () {} typeof=function 

  

looking at the definition of the above type, you may have found the law, nothing more than to unify the numeric type number, plus two JavaScript-specific function,undefined. Yes,    that's the pattern, remember this sentence

there's no problem. But knowing the exact type is not enough, you have to know what the reference type is and what the value type is. This is nothing to say, remember it: Object, function, array is a reference type, others are value types!


God-like Var statement

  To say that this var is like God, is because in most cases var defines the variable, you think it is what it is, but not all things, such as "Var res= ' 123 ' +456;", you must not tell me this is 579! var In addition to what you think is a powerful feature, it also has a role in JavaScript before the operation of the lexical parsing, can play the sound of a very advanced "variable promotion" role. Due to the lexical parsing of JavaScript, so this blog is not to explain, please pay attention to the following lexical analysis.


Scope of Elusive

  Scope! Scope! Scope! The elusive thing to say three times! It's often a good thing to do with a closed bag that sounds great. In the final analysis, the scope is closely related to the lexical parsing of JavaScript . If the article is too long to look at also sleepy, but also because of the importance of lexical analysis, so this blog post first not to explain, please pay attention to the following lexical analysis.


  After reading this blog post, if you remember the variable type of JavaScript, then we have achieved the goal.

JavaScript God-like variable system

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.