JS Basic Grammar Summary

Source: Internet
Author: User
Tags sin

Note Reprint must retain the original link (http://www.cnblogs.com/wzhiq896/p/6783296.html) wangwen896 Finishing1. Classification
ECMAScript  js基本语法与标准DOM         Document Object Model文档对象模型BOM         Browser Object Model浏览器对象模型    tips:DOM和BOM都是一套API(Application programing interface)
2. Annotation method
style   /*  */body    <!-- --!>script  //        /* */        /**        *   js说明文档注释        */
3. Simple instruction
alert("");          提示框;confirm("");        确认框,点击后会响应返回true或false;             prompt();           弹出一个输入框;document.write("");console.log("");    在控制台打印相应的信息;console.dir("");    在控制台打印出该对象的所有信息;
4. Variable naming
数字(0-9)、字母(a-z,A-Z)、下划线(_);    tips:应避免保留字和关键字;
5, Nan and isNaN
isNaN(number),如果number不是数字,则为true;Number(number),在转换为数字类型时,若number不是数字,则返回NaN;
6. Escape character
\       \r  回车\n  空格\t  缩进\\  反斜杠
7, logical short circuit, logical interrupt
true || 6;      逻辑或短路,左边为ture返回右值;6   &&  true;   逻辑与短路,左边false返回右值;
8. Priority level
    * / %    +   -    &&    ||    ?tips:自上而下优先级越来越高
9 conversion (type)
parseInt("12a3");   转为数字,尝试强转;parseFloat("123.123");data.toString();String(data);    tips:变量声明未赋值,其值为undefined;        对象为空,其值为null;
10, three-dimensional expression
eg  :   a>b?a=1:a=2;格式:    判断条件?true的时候执行的操作:false的时候执行的操作;
11. Arrays Array
(1)、定义法    构造函数:            var arr = new Array("123","abc","xxx");    字面量:            var arr = ["123","646","abc"];     数组长度:            var arr = new Array(6);(数组长度为6);(2)、赋值    arr[0]=1;
12. Formal parameters and arguments
定义函数时,function funcA(a,b,c){},其中的a、b、c即为形参;调用函数时,funcA(1,2,3);其中的1、2、3即为实参;tips:function里面有一个arguments对象,里面存有所有传进函数的实参;
13. Function functions
(1), function naming 1, you can use characters, numbers, underscores, $, 2, cannot start with a number, 3, cannot use keywords and reserved words, 4, case-sensitive, 5, the suggestion should have meaning-verb + name structure; 6, hump naming law ; 7, the function name can not duplicate, the following write the duplicate function will be written in the previous function to cover out; (2), the return value of the function return value: When the function is finished, the result is a function return value of any function has a return value of 1, in the function is not displayed in the write return, the function  The return value is undefined;2, when there is a return inside the function, but return is not followed by any content or data, the return value of the function is undefined, and the code behind the return will not be executed; 3.            When return is followed by content or data, the return value of the function is the following content or data, (3), the function of four forms: 1, no parameters, no return, usually in the encapsulation of a process, 2, no parameters, there is a return; Typically used for internal encapsulation referencing other functions (closures, callbacks), 3, with parameters, no return, usually for performing operations of the package, 4, with parameters, return, common form, (4), anonymous function anonymous function        The value of the Name property is anonymous; the function is used only once, that is, it is wasted; Eg:settimeout (function () {console.log (this.name);    },1000);            Tips: After 1 seconds, the name of the function is printed in the console, (5), the callback function in one function, another function as a parameter passed into the function, the other function is a callback function; Eg:function Atack (callback) {        return callback; } tips: When calling this function, specify which function callback is, Atack (func), (6), short-circuit operation: Prevent the incoming function of insufficient data, resulting in the inability to run; Eg:function GetResult (     A,B,FN) {       fn && fn ();        } (usually with a short circuit of logic to determine whether to execute the callback function;) function getresult_2 (A, b) {a | | 0; } (usually with a logical or short-circuit to prevent insufficient arguments, forcibly assigned values;) (7), self-executing functions (function Func2 () {}) () Tips: Write A () at the end of the function definition, and the function definition is called to execute directly after completion; (8), recursive in function Execute the last call itself again; tips: Recursion is a very resource-intensive practice, usually in order to simplify operations, but also in conjunction with caching, and note that recursion must have an end-of-judgment condition (if), otherwise the function is called after the dead loop;
14. Data type
(1)、简单数据类型    string、number、boolean(2)、复杂数据类型    String、Number、Boolean、Array、Math、Date、Obeject、function、RegExp(正则表达式)(3)、空数据类型    * Null  ---→Null的数据类型会返回一个Object    * undifined    tips:用typeof可以进行判断数据类型;    tips:定义的简单数据类型变量,其数据保存在变量中;        而复杂数据类型,其变量保存的是数据所在的内存地址;
15. Built-in objects
Array、Date、Math、String;
16. Math Object (Math)
向上取整        Math.ceil(number);向下取整        Math.floor(number);四舍五入        Math.round(number);求多个数字之间的最大值     Math.max();求多个数字之间的最小值     Math.min();求x的y次幂      Math.pow(x,y);求正弦值            Math.sin(x);    example:        求一个角度的正弦值,要求x必须是一个额弧度值        角度和弧度的转换公式:            弧度 = 角度 * 2 * Math.PI / 360;        Math.sin(30*2*Math.PI/360)Math.abs(x);    得到一个数字的绝对值
17, (array) Array object
(1), Arr1.concat (ARR2);            Array concatenation, the result is the arr2 stitching to the end of the Arr1, (2), Arr.join (); Array string output, in parentheses you can specify the element connection symbol, eg:arr=["a", "B", "C", "D"];     Console.log (Arr.join ("|"));        (The result is "A|b|c|d") (3), Arr.pop (); The last element of the array is removed, the return value is that element, (4), Arr.slice (start,end) gets, gets the specified fragment of the array, start must have, if the argument is negative, start at the end of the selection; The return value is a new array consisting of the fragment, (5), AR R.push added to add a new element to the end of the array, the parameter can be multiple, the return value is the new length of an array, (6), Arr.splice 1, which is used to add elements to the index specified in the arrays; Arr.splice (                2, 0, "William", "asdfasdf"); At the beginning of the 2nd element, the number of elements to delete (can be 0, 0 to the end), the element is "William", "Asdfasdf", 2, to replace the elements in the array; Arr.splice (2,1, "Wi        Lliam "); 3, used to delete the elements in the array; Arr.splice (2,2); (7), arr.indexof (element);        Find, find element in array, return value is index, if no element returns-1; (8), arr.sort (function);                A function of a sort; eg:function sortnumber (A, A, a) {return a-B; } arr.sort (Sortnumber);(from small to large) tips: If a-B is changed to B-A, the operation is performed from the largest to theSmall; Tips: String objects (String) are similar to array methods; 
18, (date) Date Object
date.getTime()date.getMilliseconds()date.getSeconds()date.getMinutes()date.getHours()date.getDay()date.getDate()date.getMonth()date.getFullYear()tips:很多,查文档
19, (String) object
charAt(index)str[index]          获取字符串指定位置的字符concat()        拼接字符串---------------------------slice(start,end)/substring(start,end)    截取从start开始,end结束的字符,                返回一个新的字符串,若start为负数,那么从最后一个字符开始;substr(start,length)    截取从start开始,length长度的字符,得到一个新的的字符串---------------------------indexOf(char)       获取指定字符第一次在字符串中的位置lastIndexOf(char)   获取指定字符最后一次出现在字符串中的位置trim()      去除字符串前后的空白---------------------------toUpperCase()toLocaleUpperCase()     转换为大写toLowerCase()toLocaleLowerCawse()    转换为小写---------------------------replace()       替换字符split()         分割字符串为数组
20. Custom Objects
对象:无序属性的集合;    特征:属性(key);    行为:方法(value);js是基于对象的弱类型语言;继承:基于类,子类可以从父类得到的特征;    工厂模式:定义一个function构造函数,作为对象,要创建对象直接调用该构造函数,加new关键字;构造函数:定义对象的函数,里面存有该对象拥有的基本属性和方法;    命名首字母大写,this会自动指代当前对象;访问对象属性:    obj[key];    obj.key;遍历对象:    for(key in obj){        key         为属性名;        obj[key]    为属性值(value);    }
21. JSON
{   "name" : "李狗蛋",   "age" : 18,   "color" : "yellow"}1、  所有的属性名,必须使用双引号包起来;2、  字面量侧重的描述对象,JSON侧重于数据传输;3、  JSON不支持undefined;4、  JSON不是对象,从服务器发来的json一般是字符串,通过JSON.parse(jsonDate.json)可以将其转换成js对象;
22. JS Parsing
(1)、作用域全局作用域:整个代码所有地方都可以调用;局部作用域:在函数内部声明的变量,只可以在函数内部使用;(2)、变量提升和函数提升预解析:在解析的时候,var和function都会被提升到代码的最顶端;    但是赋值操作不会被提升,定义和函数才会被提升;    if里面的变量定义也会被提升,但是赋值操作不会;
23. Other details (tips)
(1)、元素由对象组成的数组进行排序    eg:        var data = [            {title: "老司机", count: 20},            {title: "诗人", count: 5},            {title: "歌手", count: 10},            {title: "隔壁老王", count: 30},            {title: "水手", count: 7},            {title: "葫芦娃", count: 6},        ];            //该数组的元素都为对象。我们需求为根据count的值给数组重新排序。            //解决方案:使用sort方法,对传入的函数做手脚。        function sortArr(a,b){            return a.count > b.count;           }        data.sort(sortArr);            //原本的a和b的比较方法变成a.count和b.count;            //原本的比较方法可以参见17,数组对象            //至此,data将以count值从小到大排列。    tips:Array对象的sort方法传入的为比较函数,比较函数里return排序比较的方法;        原始的sort方法传入的函数内部return的值为a>b,        通过修改sort传入的函数,可以实现对元素为对象的数组的排序!

JS Basic Grammar Summary

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.