JS basics Daquan

JS basics

The basic syntax
* switch cycle (provided the expression x (typically a variable) values would then each case with the structure of expression compared. If there is a match, then the case will be executed with the associated block. use break down automatically to prevent a case the code is running) is not the type conversion

var x;
switch(x){
case true:console.log("x类型转换")   ;
default:console.log("x没有发生类型转换");
 }//case没有发生类型转换    switch语句执行严格相等

Ternary operator *

var a=1;
console.log(a?"a has a value":"a not has a value");//a has a value
 //利用三元运算符,输出相应提示
n=2;
 var msg="数字"+n+"是"+(n%2===0?"偶数":"奇数");//偶数
  console.log(msg);*/
   //利用三元运算符,在字符串之间插入不同的值

* Break and continue statements are automatically closed or out of a block of code

var num = 0;
       for (var i = 0 ; i < 10 ; i++){
           console.log("i="+i);
           for (var j = 0 ; j < 10 ; j++){
               console.log("j="+j);
               if( i == 5 && j == 5 ){
                   console.log("i,j=5");
                   break;
               }
               num++;//95
           }
       }
       console.log(num);//当i,j=5时,break会结束j的循环,但是不会影响i的循环
var num=0;
       for(var i=0;i<10;i++){
           console.log("i="+i);
           for(var j=0;j<10;j++){
               console.log("j="+j);
               if(i==5&&j==5){
                   console.log("i,j=5");
                   continue;
               }
               num++;//99
           }
       }
       console.log(num);//当i,j=5的时候,continue会自动跳出这个循环,继续下一个

* Label tag

var num = 0;
       outPoint://这里为label,标签名为outPoint
           for (var i = 0 ; i < 10 ; i++){
               console.log("i="+i)
               for (var j = 0 ; j < 10 ; j++){
                   console.log("j="+j)
                   if( i == 5 && j == 5 ){
                       console.log("i,j=5")
                       console.log(num)
                       break outPoint;
                   }
                   num++;//55
               }
           }
       console.log(num);//有label标签之后,break会结束任何的代码块循环,所以当i,j=5的时候,break结束了i,j的循环,只循环了55次
var num = 0;
       outPoint:
           for (var i = 0 ; i < 10 ; i++){
               console.log("i="+i)
               for (var j = 0 ; j < 10 ; j++){
                   console.log("j="+j)
                   if( i == 5 && j == 5 ){
                       console.log("i,j=5")
                       console.log(num)
                       continue outPoint;
                   }
                   num++;//95
               }
           }//同理continue会跳出i.j的循环

Data type
JavaScript has six data types: a character (string), the object (object), undefined, null, the value (number), the Boolean value
while ES6 new Symbol () type
three methods of JS in the value type is determined:
1 : typeof operator
for the array, null, etc. Object returns, typeof only area type value, can not distinguish between a reference type, only to give six kinds of typeof value: number, object, string, function , undefined, Boolean.
2: The method of that type is the constructor for determining reference instanceof

var p =new Person();
console.log(p instanceof Person);//true``

. 3: Object.prototype.toString ()
null and undefined
null indicating "empty" means, if the converted value becomes 0, undefined undefined here indicates, if the converted value, returns NaN
Boolean
following operators It returns the boolean value: two and unary &&, ||, or; pre operator! Non, comparison operators (<, <=,>,> =), equality operator (==,! ==,! =, ==)
only six values are converted to false (null, undefined, NaN, 0, false, null character)
value of
integer and floating point
all the figures are based on 64-bit floating-point number is stored in JavaScript, 1.0 and 1 are equal, the underlying language is not an integer
value range
can be represented by a numerical range 1024 -1023 th power of 2 to the power 2
occurs positive overflow, with digital infinity (infinite) is
negative overflow, with digital -Infinity (minus infinity)
at the same time, providing a subject and number in MAX_VALUE MIN_VALUE maximum and minimum
ary
binary: 0b or preceded 0B, consisting of 0,1
octal: preceded 0o, 0O, or with leading 0, and there are 0-7 numbers consisting
decimal: 0 no prefix
hexadecimal system: prefix 0x or 0X
NaN3
NaN belong to the type of number, digital parsing errors when there will be, will get 0 divided by 0, NaN is not equal to any number, just equal to its own, and NaN operation between any number will get NaN, indexof use the array operator is strictly equal, so this method can not be used NaN
* associated conversion method
parseInt:
the method for string to an integer. The return value is only two possibilities, either a decimal integer, either NaN.

1) If the string head spaces, spaces will be removed automatically.
2) If parseInt argument is not a string, it will then convert it to a string first.
3) string to an integer when a character is followed by conversion, if they are not converted to numeric characters, no longer proceed, return has been transferred to the good parts.
parseFloat ():
convert a string to a float, if the parameter is not a string or the string of the first character can not be converted to a floating returns NaN, it should be noted that an empty string will be converted to NaN
isNaN:
NaN judgment is not used, there returns true strings, arrays, NaN
alternative methods: with itself varies as NaN itself, it is possible to write a function

function   msgNaN(value){
            return value !==value
}

isFinite ():
used to determine if a number is not a normal number, in addition to positive infinity, negative infinity, NaN returns false, the rest will return true.
String
string can not write multiple lines, will complain, if there is a long string, backslash can never finish line in the final
special use of the backslash: the JS basics Daquan
string is equivalent to an array of characters, you can use numbers in square brackets operator, returns a location number, if the number is greater than the length of the string or not a number inside the square brackets, will return undefined
length properties: the length of the string can be read out method, the method can not be rewritten
character set : JavaScript use Unicode character set. Internal JavaScript engine, all characters are represented by Unicode. JavaScript is not only to store Unicode characters, but also allows the use of Unicode code points directly in the program represents the character, the character is about to be written in the form of uxxxx \, where xxxx represents the Unicode code point of the character. For example, \ u00A9 on behalf of the copyright symbol.
A code point U + U + 10000 to characters between 10FFFF, JavaScript that they are always two characters (length attribute is 2). So when treatment must take this into account, that is to say, JavaScript returns the string length may be incorrect.
Base64 Transcoding: To avoid problems of special characters into any value 0-9, az, AZ, +, /, of the 64 characters
btoa () values were converted to arbitrary Base64
atoB () Base64 into the original value
However, these two methods is not applicable ASCII value, Base64 ASCII converted to an intermediate transcoding process to add a
JS basics Daquan
subject
If the key name does not meet the conditions of the distinguished name (for example, the first one is spaces, numbers, or the operator), and not a number, it will error, this time to add quotation marks and
JS basics Daquan
references to objects:
If different variable names point the same object, that is, will point to the same address, modify the value of one, the other value will be modified
JS basics Daquan
, but if the deletion of the reference to the original name of a variable object, another object does not modify the
JS basics Daquan
expression or statement:
If the beginning are braces, is the code block, if the if the object on it is necessary with parentheses
JS basics Daquan
property
attributes read: If the bracket operator, must be quoted for Jianming, otherwise they will be treated as a variable
JS basics Daquan
value of the key name is not used dot operator, otherwise it will error, can only use the square brackets operator
JS basics Daquan
to view all the properties of the object, the method of Object.keys
JS basics Daquan
delete attributes:
the delete to delete the object properties, returns true after the deletion, the application of the time, do not delete the property also exists returns true, it is not according returns true, it is determined that there is the attribute of the object, only Genera exist, and can not be deleted when it returns false; at the same time can only delete their own property, you can not remove the inherited property
JS basics Daquan
in operator
to determine whether there is a property in which the object will return true if there is, but this method can not be judgment itself or property is inherited
JS basics Daquan
for in to loop through all the properties of an object, he traversed can traverse objects are objects, not traverse the object can not be traversed, and not only will traverse their own property, will traverse the inheritance the property, use for ... in time, the use hasOwnProperty methods should be combined, in the inner loop to determine what, if a property is a property of the object itself. JS basics Daquan
with statement
JS basics Daquan
If there is an internal block with variable assignment must be current object attribute that already exists, otherwise it will create a current scope of global variables.
JS basics Daquan
An array of
any type of data, can be placed on the array
length property
array's length property is writable, length change may increase or decrease the length of the array, the array length set to 0, can be used to empty array, the array length is greater than if the original set length of the array, then the position will add extra space, the new position will return undefined, the nature of the object is an array, it is possible to artificially add an attribute array, without changing the length of the arrayJS basics Daquan
in operator
testing a key name whether there is in the array, that is applied to the object, but also for an array
* for in loop and array traversal
will not only traverse the array of number keys, but also through the array of non-numeric keys
is recommended to use a while loop for loop as well as in the array or a method foreach

<body>
<p>点击按钮计算数组所有元素相加的总和。</p>
<button onclick="numbers.forEach(myFunction)">点我</button>
<p>数组元素总和:<span id="demo"></span></p>
<script>
var sum = 0;
var numbers = [65, 44, 12, 4];
function myFunction(item) {
    sum += item;
    demo.innerHTML = sum;
}
</script>//数组元素总和:125

数组的空位
数组的空位是可以读取的,并且返回undefined,同时数组的空位在forEach,for in,以及Object.keys的方法遍历的过程中会跳过,但是如果某个位置是undefined在遍历的过程中就不会被跳过。
类数组的方法
slice可以将类似数组的方法变成真正的数组
var arr = Array.prototype.slice.call(arrayLike);
通过call可以把数组的方法加在对象上面
JS basics Daquan
函数
函数可以调用自身,就是递归
变量提升:
如果采用function和赋值语句同时调用同一个语句,最终会采用赋值语句的方法
JS basics Daquan
不能在条件语句中声明代码块,但能运行
JS basics Daquan
函数的属性和方法
name属性:返回函数的名字,也可以返回函数参数的名字
JS basics Daquan

length
函数的length属性返回函数预期传入的参数个数,length属性提供了一种机制,判断定义时和调用时参数的差异,以便实现面向对象编程的”方法重载
tostring
返回一个字符串,内容是函数的源码
JS basics Daquan
*函数的参数
函数的length属性是只读属性,代表函数形参的数量,也就是在函数定义时给出的形参个数。需要注意的是,函数的length属性与实际传入的参数个数无关,只反映函数预期传入的参数个数。
没有办法只省略靠前的参数,而保留靠后的参数。如果一定要省略靠前的参数,只有显式传入undefined。

function f(x,y){}
f.length  //2

函数的传递
原始类型的值(数值、字符串、布尔值),传递方式是传值传递(在函数体内修改参数值,不会影响到函数外部。)
参数是复合类型的值(数组、对象、其他函数),传递方式是传址传递(内部修改参数,将会影响到原始值。)
注意!如果函数内部修改的,不是参数对象的某个属性,而是替换掉整个参数,这时不会影响到原始值。
JS basics Daquan
*arguments 对象
实参对象,只在函数内部使用
通过arguments的length属性可以知道函数在调用的时候到底带几个参数
与数组的关系:虽然arguments很像数组,但它是一个对象。数组专有的方法(比如slice和forEach),不能在arguments对象上直接使用。如果要让arguments对象使用数组方法,真正的解决方法是将arguments转为真正的数组。两种常用的转换方法:slice方法和逐一填入新数组。
callee 属性:arguments对象带有一个callee属性,返回它所对应的原函数。JS basics Daquan
闭包:
1.读取函数内部的变量,并让这些变量始终保持在内存中,即闭包可以使得它诞生环境一直存在。
JS basics Daquan
2.封装对象的私有属性和私有方法。
JS basics Daquan
注意,外层函数每次运行,都会生成一个新的闭包,而这个闭包又会保留外层函数的内部变量,所以内存消耗很大。因此不能滥用闭包,否则会造成网页的性能问题。
eval 把字符串当成语句来执行放在eval中的字符串有独立存在的意义,不能放在eval之外使用,他没有自己的作用域,都在当前的作用域因此可能会修改当前作用域的值

eval('var a=1');
a  //1

与eval相类似的JS basics Daquan

JavaScript规定,如果使用严格模式,eval内部声明的变量,不会影响到外部作用域。

(function(){
  'use strict';
  eval('var a=1');
  console.log(a);  //ReferenceError: a is not defined
})();

运算符
JS basics Daquan
数值运算符:可以将任何值都转化为数值和number的属性相同
JS basics Daquan
字符串的比较:
在比较字符串的时候,首先比较字符串第一个字符的Unicode码点,如果相等,就比较第二个,以此类推
非字符串的值
原始类型的值:先转化成数值再比较
对象:如果运算子是对象,会转化为原始类型的值,在进行比较
对象转化为原始类型的值,算法是先调用valueof的方法,如果返回的还是对象,再调用tostring方法
(3)undefined == null // true; undefined === null //false
逗号运算符
逗号运算符用于对两个表达式求值,并返回后一个表达式的值。
数据类型转换
强制转换
number()将任意值转化为数值
参数为原始类型的值,只要有一个字符无法转换,就会返回NaN,当参数为对象时,会返回NaN,除非是包含单个数字的数组
JS basics Daquan
string()
原始类型的值转换:数值转化为相应的字符串
字符串转化为原来的值,布尔值:true转为字符串"true",false转为字符串"false"。undefined:转为字符串"undefined"。null:转为字符串"null"。String方法的参数如果是对象,返回一个类型字符串;如果是数组,返回该数组的字符串形式。
boolean():undefined,null,NaN,"",0这五个值会转化为false
所有对象(包括空对象)的转换结果都是true,甚至连false对应的布尔对象new Boolean(false)也是true
JS basics Daquan
自动转换:!! expression
对象类型转换:
valueof:valueOf()方法。如果存在任意原始值,它就默认将对象转换为表示它的原始值;对象是复合值,而大多数对象无法真正表示为一个原始值,因此默认的valueOf()方法简单地返回对象本身,而不是返回一个原始值
undefined和null没有valueOf()方法

undefined.valueOf();//错误
null.valueOf();//错误

布尔型数据true和false返回原值。布尔型数据的包装对象返回true或false

true.valueOf();//true
typeof true.valueOf();//'boolean'
false.valueOf();//false
typeof false.valueOf();//'boolean'
Boolean.valueOf();//Boolean() { [native code] }
typeof Boolean.valueOf();//'function'

字符串类型原值返回。字符串类型的包装对象返回字符串值
数值类型分为整数和浮点数进行处理。数值类型的包装对象返回数值类型值
整数直接跟.valueOf()形式,会报错,提示无效标记,因为整数后的点被识别为小数点,所以尽量加括号

0.valueOf();//Uncaught SyntaxError: Invalid or unexpected token
(0).valueOf();//0
+0.valueOf();//Uncaught SyntaxError: Invalid or unexpected token
(+0).valueOf();//0
-0.valueOf();//Uncaught SyntaxError: Invalid or unexpected token
(-0).valueOf();//-0

浮点数原值返回

1.23.valueOf();//1.23
+1.23.valueOf();//1.23
-1.23.valueOf();//-1.23
NaN.valueOf();//NaN
Infinity.valueOf();//Infinity
-Infinity.valueOf();//-Infinity

对象Object类型及自定义对象类型返回原对象

{}.valueOf();//报错,Unexpected token .
({}).valueOf();//Object{}
typeof ({}).valueOf();//'object'
({a:123}).valueOf();//Object{a:123}
Object.valueOf();//Object() { [native code] }
typeof Object.valueOf();//'function'

函数Function类型返回原函数


function test(){
    alert(1);//1
}
test.valueOf();/*function test(){
                    alert(1);//1
                  }*/
Function.valueOf();//Function() { [native code] }

tostring
undefined和null没有toString()方法
布尔型数据true和false返回对应的'true'和'false'
字符串类型原值返回
1、正浮点数及NaN、Infinity加引号返回

1.23.toString();//'1.23'
NaN.toString();//'NaN'
Infinity.toString();//'Infinity'

  2、负浮点数或加'+'号的正浮点数直接跟上.toString(),相当于先运行toString()方法,再添加正负号,转换为数字

+1.23.toString();//1.23
typeof +1.23.toString();//'number'
-1.23.toString();//-1.23
typeof -1.23.toString();//'number'

  3、整数直接跟上.toString()形式,会报错,提示无效标记,因为整数后的点会被识别为小数点

0.toString();//Uncaught SyntaxError: Invalid or unexpected token

  Accordingly, in order to avoid the above situation and an error is invalid, when using digital toString () method, solve the brackets

(0).toString();//'0'
(-0).toString();//'0'
(+1.2).toString();//'1.2'
(-1.2).toString();//'-1.2'
(NaN).toString();//'NaN'

Digital type toString () method may be receiving a radix conversion (the radix) optional parameter, if this parameter is not specified, the conversion rule is based on the decimal. Similarly, it may be converted to other hexadecimal number (range 2-36)

var n = 17;
n.toString();//'17'
n.toString(2);//'10001'
n.toString(8);//'21'
n.toString(10);//'17'
n.toString(12);//'15'
n.toString(16);//'11'

Guess you like

Origin blog.51cto.com/14419253/2435960