JavaScript base (2) - the type, values and variables

  This chapter is more important chapter in JavaScript, so the size will be longer, please be patient to read.

  In programming languages, types, values ​​and variables are the most basic concepts, all of the programming languages ​​contain these three terms, before the details of them, we implemented progressively, with the most simple example recap type, value and what variables are yes.

var msg = 'hello world'

   In the embodiment, MSG that we define the variable , 'Hello World' is the value , he is assigned to the variable MSG, ' Hello World' is a string type .

   After three simple understanding of the concept, you can enter the topic, Again, this is in addition to "javaScript Definitive Guide 6th Edition," the summary and conclusion, there are many parts to expand and give up, do not know how friends can turn it original, you will find that you can not read more.

 

1. Outline Data Type

  We usually JavaScript data type is divided into two main categories: primitive types and object types.

  Primitive types contain numbers, strings, Boolean type and two special original value the null (empty) and undefined (indicating).

  Objects (object) is a collection of attributes (Property) of each attribute of "Name" and "Value" configuration, i.e. key-value pairs, Common JavaScript object is an unordered set of key-value pairs, but defines a JavaScript special ordered set, ie, the array, so the array is "key to orderly" special object, we will detail later in the next array. JavaScript also defines a special object - function, the function is an object with executable code, his behavioral characteristics and other objects are not the same, you can even be understood as - the function is the function (temporarily confuse the functions and objects ), a function of part, I will ES6 series of relationships Class and constructors of detail in the section (in fact, I have not written), after reading can help you understand why the function can be called an object.

   Therefore, we can say that JavaScript data types integrated into six categories: number, string, boolean, null, undefined and objects. Of course, now ES6 on the basis of the type of data the symbol of this new basic data types, are interested can find out yourself.

 

2. Why say JavaScript is an object-oriented language

  JavaScript is an object-oriented language, for most people it's just a slogan, many people do not understand what is the meaning of this sentence, namely: Why say JavaScript is an object-oriented language.

  We look at how the original is to say: JavaScript as an object-oriented language, means that we do not have globally defined function values of different types of operation, types of data itself can be defined method (method) to use the value, for example, to the array a sort of elements, we do not have to pass a sort () function, but a call to a method sort ().

a.sort() //sort(a)的面向对象版本

   This is at least the third time I look at this text, I have read, How about you?

   In fact, to understand object-oriented, we can start from the other side, for example, why the process is called C language-oriented language. In the C language, when we need to do a certain type of a common data structure (such as sorting) operation, how should we do?

  We need to define a global function sort (), the array is then passed as a parameter sort () function, after sorting, return a result, the following example:

//面向过程
function sort(arr){
  ... //将传入的数组进行排序
  return newArr
}
var a = [2,1,3]
a = sort(a)

  Therefore, process-oriented can be summed up as, you want to operate a certain value, the value must be passed pre-defined function to output the results you want.

  Understand the process-oriented, object-oriented then understand much easier. When you need to manipulate an object, you can use the direct method of the object inside the object to operate, and get the results you want, without having to predefine a global function. The following example:

var a = [2,1,3]
a.sort() // [1,2,3]

  

3. The original type of immutability

  Five original type (numeric, boolean, null, undefined, and string) in the JavaScript are immutable, so you can not change the data type of the variable into the variable types (objects) and immutable.

 It should be noted that the variable here is the value for the immutable , rather than variable, when we enter a piece of code

var a = 1

  a variable as a variable, a variable can become ++ 23456, can be transformed into a character '1', or become a Boolean true, but a value in itself will not change, you can not 1 by any of the operation becomes zero. Why would I want so much emphasis on immutability 1, because in the face of a string, we tend to think the string is variable. Consider the following code

var str = 'hello world'
console.log(str.toUpperCase()) //HELLO WORLD
console.log(str) //hello world

  After a string of code is operating returns a new string, the string itself will always remain the same, of course, the mystery of this code there are many, such as why the original string as the type of the object can be invoked to call the method, which is a question we have to answer in the back of the wrapper object inside, where you just know, you can not go to change the value of an immutable type by any method .

 

4. What object is a package?

  JavaScript object is a composite value: he is a named property or a collection worth. To refer to the property's value. "" Sign, when the property value is a function too, we can be invoked through obj.fn () .

  Na mud horse are you talking about? I really do not understand what he meant.

  It left a doubt that when we are talking about just an immutable characteristic string: Why string as the original type, you can use the calling object type to be able to use? Let us step by step to answer this question:

  Step: var str = 'hello world'; the left side of the equal sign, which we call variable, the right hand side, we call the value, type value, a character string.

  Step two: value called value addition, it is also known as: literal. "Literal" understanding is that in addition to the literal meaning still deeper meaning literally.

  The third step: '' so 'hello world' string is just a face, when he uses symbolic references toUpperCase () function of time, he is not a string, JavaScript surreptitiously to call new String (str) turned him into an object, the object inherits the methods and properties of the string. Once the reference end, the newly created object was summary execution, so you can then print str, he still print a string, not an object. (Of course, the book also said, JavaScript is only the surface behave this way, specific Which, I ask who you ask me)

  So we can understand wrapper object: When immutable types need to be operated, JavaScript creates a temporary object to support the immutable type of "routine operation", when the temporary object is no longer referenced by the destruction, not change the type or immutable, just had nothing happened.

 

The particular type of null and undefined layman

  null and undefined are described in "null", JavaScript interpretation null and undefined are very subtle.

  undefined represents system-level, unexpected or similar errors worth vacancies.

  null indicates that the program level was normal, was expected vacancies.

  If you do not understand the meaning of these two words have to show that you like me before, I feel like a fart these two words, what was unexpected vacancies, vacancies what is expected?

  To understand these two words, I give you from a personal point of view some help, let's think about a problem:

 Please print a console and a null undefined .

  Here I offer ten thousand kinds of ways to print undefined

  var a
  console.log(a)
  function b(c){
  	console.log(c)
  }
  console.log(b())
  var d = {}
  console.log(d.name)

  Above, a, b, c, d are printing undefined, thus undefined surface is uninitialized variables, or the values ​​of attributes or elements of an array of objects that you want to query does not exist, or the shape function parameters do not provide arguments, or function no return value, etc., etc., can be printed out undefined.

  So the question is, how does print a null?

  I just find a way to print null, that is,

var a = null
console.log(a)

  Yes, that is a direct null variable assignment, so we call it, to be expected, because you do not assign can not have a value equal to null, the null must be artificial.

  It solves the problem of how to print null and undefined in the console after you look back at the interpretation of JavaScript, if like me enlightened na?

 

5. little understanding of the object reference

  What is the object of reference?

  Here we must start with the comparison value. Code and the output of view have obtained the following results:

var a = 'hello'
var b = 'hello'
console.log(a === b) //true
var c = {name:'hello'}
var d = {name:'hello'}
console.log(c==d) // false
var e = [1]
var f = [1]
console.log(e==f) //false
console.log(e[0]===f[0]) //true

  As can be seen from the above example, the two objects appear equal, but in reality is different, that is not worth comparing comparison, even if two objects contain the same properties and the same value, they are not equal to the object .

  So when the object will be equal to it?

var a = [1]
var b = a
b[0] = 3
console.log(a) //[3]
console.log(a===b) //true

   We can see that when the object is assigned to b a, b change will also affect the value of a value, in which case you can think of, a, b completely equal, because they point to the same piece of memory address .

  Computer concepts do not quite understand children's shoes may not understand what a point to the same memory address, let's break steps to create a JavaScript object

  Step: JavaScript found that a variable is assigned to an array (specific object), so the JavaScript application to computer memory space allocated to a piece of land, the land may be continuous (arrays), or may be discontinuous (common objects), but whether he does not even continuous, every piece fields can only plant the same thing - if a [0] = 'sweet potato', then a [0] will not be equal to the potatoes, you can in a [1] species potato.

  Step Two: After a initialization is complete, this variable will get his computer to an address, which is the subject of a address is where to start, where a note just to get the first piece of land can address (you can be understood as the first piece of land attached to the second block, the second block attached to the third piece of land, so there is no need to worry about the back of the address can not be found, because they are not even in the together).

  Step Three: Case 1 - JavaScript found b variable is also assigned an array, the array is an array of new, how to do it? JavaScript had to give an application to b, b to carry out farming on this land, this time a first piece of land planted potatoes, b is also the first piece of land to grow potatoes, two pieces of land but after all, not a piece of land, it is inconsistent with their address, which also explains why not the first example of two properties exactly equal objects are equal, because they are not point to the same memory space. 2-- case newcomer is assigned a variable b, then b will get a address at this time no matter you let go b farming, farming still make a go, they modify the values ​​are the same memory space Therefore a and b are completely equal, because they point to the same piece of memory.

  Therefore, the object reference refers to is that when you put an object variable to another variable is, in fact, these two variables point to the same address, which means that they are completely equal a target. That is, a man and two women, you know what I mean?

 

  This chapter is the original content of the third chapter, personally I think that there is a better implicit type conversion and explicit type conversion , the global object concepts, etc., with regard to variable scope and variable lift, function scope and some other very important, but Since ES6 broke the rules function scope, there is not too much to explain outdated knowledge, and interest themselves about at the top of the keyword, like bloggers (not like the front end can be) female fans can be added to the official fan base 708 637 831

Published 109 original articles · won praise 196 · Views 300,000 +

Guess you like

Origin blog.csdn.net/dkr380205984/article/details/90199432