JavaScript authoritative Guide Reading notes-3rd chapter types, values and variables (1)

Source: Internet
Author: User

Have always had an idea, well read JS authoritative guide, easy for their own JS have a more comprehensive understanding. After all, I am not a computer professional born, although doing related industry work, but always feel that the master of the Foundation and no relevant professional scholars solid, just because of resignation and other reasons, or determined to read this thick ' dictionary '. In the process of reading the original book, I also found a lot of conceptual problems, may be because of the differences between the Chinese, leading to the actual situation and the concept of the description is not consistent with the case, here is all as the next note.

The record from the third chapter of the book, if the lucky someone saw my blog and interested in the first two chapters, can be understood through other means, here as the authoritative guide concept of the reduced version, as well as their own unemployed to motivate themselves to persist in a way.

3. Types, values, and variables

  JavaScript's data types fall into two categories, primitive types (or primitive types), and object types (or reference types).

  Where the prototype type includes numbers, strings, Booleans , and two special primitive values null and undefined five classes, the object type is the common unordered name value pair {a:1,b:2}, Numbered ordered set--array [1,2,3,4], and function functions ab () {xx}.

  An object is a collection of properties, each of which consists of a name/value, which can be the original value, such as the number {a:1}, the string {A: ' nice '}, or the object [{A:1},{b:2}].

  A function is an object with executable code associated with it, a wrapper for a class method, a calling function that runs the execution code, and returns the result of the operation.

If the function uses the new operator to initialize a new object, which is called a constructor, each constructor defines a collection of objects initialized by the constructor. For the use of the new operator, in addition to a new array, new, we can also new a date date,new a regular expression regexp as well as the wrong class error.

  garbage collector mechanism : JS interpreter has its own memory management mechanism, can automatically release memory in a timely manner, when an object is not required by any local guidance, the interpreter will automatically release it, so as to reclaim the memory resources it occupies. We can understand that when eating in a fast food restaurant, the aunt who cleans the table will always be able to clean up the table that the guest has just left, so that the guests can continue to use it (memory release).

  JavaScript is an object-oriented language , simply put, we do not need to define global functions to manipulate different values, but the data itself to use methods, such as array a ordering, we do not need to pass a into the function sort (), like this sort (a); Instead, use a call method directly, like this a.sort (), where A.sort () is the object-oriented version of sort (a).

Technically, only JS objects can have methods, but numbers, strings and Boolean values can also have their own methods, such as string conversion, array cutting, etc., in JS only null and undefined is no method can use the value.

  As we said before, JS data types can be divided into primitive types--strings, numbers, Booleans, null,undefined and object types--functions, arrays, etc., can also be divided into the type of the owning method and the non-owning method, which says, null and undefined are not available methods , the rest can be, of course, we can also divide the data into mutable types and immutable types, the prototype data type is immutable, the object type is variable, for example, we can modify the length of an array, add a child element to it, and so on. Here we will wonder, the string is not also can read length, get and replace bytes, in fact, for the method of string, all the operation of the string is a new variable returned, not have modified the original string, as follows:

var ' string ' ,     = Str.replace ('s','s'); Console.log ('  modified to ' +str,' modified to '+str1 ')

It can be seen that the original string has not been modified, similar numbers are the same, although there are corresponding methods, but these methods do not affect itself.

  JS variable is untyped, straightforward point, the variable can be given any type of value, after assigning a type value can be arbitrarily modified to other types of values, such as I declare a variable var a= 1, when the value type of variable A is a number, we immediately use a= ' str ', Then the value type of variable A is replaced by a number as a string type. The use of Var in JS to declare a variable, in the ES6 we also know the let and the constant declaration of const, these follow-up to describe.

JS uses lexical scopes, variables are divided into global variables and local variables . Variables that are not declared in the body of any function are called global variables, which can be called anywhere in the JS program , such as:

console.log (num) var num =1;

Output is undefined, and will not error, here is involved in the variable promotion. As we said earlier, Var num is not declared in the function body, there is nothing to limit it, although it is declared behind the console, but NUM is ubiquitous, in fact, its figure in the entire program can be called, but that Num's assignment is in the console , but NUM declarations can be changed anywhere, everywhere, and everywhere. The above code is equivalent to

var=1;

While local variables appear to be more reserved than global variables, in the function of the divine variables, it can always be visible in the function body, outside the function is not authorized to access the variable. As follows:

function num () {    console.log (nummber)    var nummber =1;} Num (); // undefinedconsole.log (nummber)// error

It can be understood that the function num is a small world, the variable number can be arbitrarily called relative to the function num, but outside the function body, we are not authorized to directly access the use of this variable.

  

  

 

JavaScript authoritative Guide Reading notes-3rd chapter types, values and variables (1)

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.