javascript learning 3, Data Types

Data types include: basic data types and the reference data type

Refers to basic data types are simple segment data, reference data type means having a plurality of target values ​​thereof.

When we assign a variable to a variable, the parser is the first to confirm this value is a basic type value or a reference type value

A basic data type

1. number

A = 123 var; 
// this variable typeof check what type of data 
the console.log (typeof A) 
// special case 
var A1 = 5/0; 
the console.log (typeof E1) Infinity // infinite number type.

2. string

var str  = '123'
console.log(typeof str)

3. boolean

var b1 = false;
console.log(typeof b1)

4. null

var c1 = null;. // null object Object 
the console.log (C1)

5. undefined

D1 var; 
// represents a variable defined 
console.log (typeof d1)

 

II. Common built-in properties

String -> equivalent Python string
Array -> Python equivalent list
Object -> equivalent of Python Dictionary
Function -> equivalent Python function
Date -> Python time equivalent

The so-called built-in objects that provide ECMAScript out some of the objects, we know that objects are corresponding properties and methods

 

1. String String

String Methods

 

Character (1) chartAt () Returns the index position

var str = 'alex'; 
var charset = str.charAt (1); 
console.log (charset); // l

(2)  the concat returns a string value representing the splicing of two or more strings

was str1 = 'al'; 
was str2 = 'ex'; 
console.log (str1.concat (str2, str2)); // Alexex

(3) replace (a, b) will be replaced by a string string b

var a = '1234567755';
var newStr = a.replace("4567","****");
console.log(newStr);//123****755

(4) indexof () Find subscript characters, if the return of the index to find the string, not found -1 is returned. With seach () method uses the same

var str = 'alex';
console.log(str.indexOf('e'));//2
console.log(str.indexOf('p'));//-1

(5) slice (start, end) to extract part of a string and returns a new string. Opening and closing the left and right divided string

var str = 'Mr Ma'; 
the console.log (str.slice (1,2)); // MA

(6) split ( 'a', 1) split the string string a, and returns the new array. If the second parameter is not written, which returns the entire array, if the number is defined, the maximum length of the array is returned

var str = 'My God it, is a thing, are you talking about a ha ha ha?'; 
console.log (str.split ( 'a')); // [ "My God does," " Yes Well, are you talking about? "," ha ha ha "]

(7) substr (start, length) Returns a string of characters starting at the specified position to the specified number of characters. 

var str = 'My God it, is a thing, are you talking about a ha ha ha?'; 
console.log (str.substr (0,4)); // my day it

(8) toLowerCase () small letter

var str = 'XIAOMAGE';
console.log(str.toLowerCase());//xiaomage

(9) toUpperCase () turn uppercase

var str = 'xiaomage';
console.log(str.toUpperCase());

(10) substring (indexStart, indexEnd ) extract the string of characters between two intermediary specified index. Returned substring includes  the beginning of  the character, but not including the  end of the  character at the

  • If  indexStart equal  indexEnd, substring it returns an empty string.
  • If omitted  indexEnd, substring extract characters until the end of the string.
  • If any of the arguments is less than 0 or  NaN, it is treated as zero.
  • If any parameter is greater than  stringName.length, it is treated as  stringName.length.
  • If  indexStart greater than  indexEnd, the  substring implementation of the results as the same two parameters swap

(11) trim () remove both sides of the blank string

particular:

// converts a number to a string type Type 
var NUM = 132.32522; 
var = numstr num.toString () 
the console.log (typeof numstr) 

// rounded
var = newNum num.toFixed (2)
the console.log (newNum)

 

2. Array Array

(1)  to create an array of ways

  • Creating amount literal way (we recommend using this method, simple and crude)
var colors = ['red','color','yellow'];
  • Created with the constructor (later speak) way to use new keyword to create an object constructor
var colors2 = new Array();

(2)  array-valued

ARR = var []; 
// index eleven assigned by 
ARR [0] = 123; 
ARR [. 1] = 'ha'; 
ARR [2] = 'Hey Hey'

(3)  conventional array-

The combined concat (3.1) of the array ()

var north = [ 'Beijing', 'Shandong', 'Tianjin']; 
var South = [ 'Dongguan', 'Shenzhen', 'Shanghai']; 
        
var newCity = north.concat (South); 
console.log (newCity)

(3.2)  the Join () array element of the specified strings together, it will form a new string

var score = [98,78,76,100,0];
var str = score.join('|');
console.log(str);//98|78|76|100|0

(3.3) to the array into a string toString ()

Score = var [98,78,76,100,0]; 
// toString () using direct conversion between each element of the string comma 
           
var = score.toString STR (); 
the console.log (STR); // 98,78,76,100,0

(3.4) slice (start, end); returned array section, left and right to open and close

var arr = ['张三','李四','王文','赵六'];
var newArr  = arr.slice(1,3);
console.log(newArr);//["李四", "王文"]

(3.5) pop 删除数组的最后一个元素并返回删除的元素

var arr = ['张三','李四','王文','赵六'];
var item = arr.pop();
console.log(arr);//["张三", "李四","王文"]
console.log(item);//赵六

(3.6) push() 向数组末尾添加一个元素或多个元素,并返回新的长度

var arr = ['张三','李四','王文','赵六'];
var newLength= arr.push('小马哥');//可以添加多个,以逗号隔开
console.log(newLength);//5
console.log(arr);//["张三", "李四","王文","赵六","小马哥"]
 

(3.7) reverse() 翻转数组

var names = ['alex','xiaoma','tanhuang','angle'];
       
//4.反转数组
names.reverse();
console.log(names);

(3.8) sort对数组排序

var names = ['alex','xiaoma','tanhuang','abngel'];
names.sort();
console.log(names);// ["alex", "angle", "tanhuang", "xiaoma"]

(3.9) 判断是否为数组:isArray()

 布尔类型值 = Array.isArray(被检测的值) ;

(3.10) shift() 删除并返回数组的第一个元素

(3.11) unshift() 向数组的开头添加一个或更多元素,并返回新的长度

 

3.object

 

4.Function函数

函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句。

函数的作用:

  • 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动。

  • 简化编程,让编程模块化。

console.log("hello world");
sayHello();     //调用函数
//定义函数:
function sayHello(){
  console.log("hello");
  console.log("hello world");
}

1.函数的定义

函数定义的语法:

function 函数名字(){
    函数体
return 返回值 }

解释如下:

  • function:是一个关键字。中文是“函数”、“功能”。

  • 函数名字:命名规定和变量的命名规定一样。只能是字母、数字、下划线、美元符号,不能以数字开头。

  • 参数:后面有一对小括号,里面是放参数用的。

  • 大括号里面,是这个函数的语句。

2. 函数的调用

函数调用的语法:

函数名字();

3. 形参和实参

函数的参数包括形参和实参

注意:实际参数和形式参数的个数,要相同。

sum(3,4);
sum("3",4);
sum("Hello","World");

//函数:求和 function sum(a, b) { console.log(a + b); }

4. 函数的返回值

console.log(sum(3, 4));
    //函数:求和
    function sum(a, b) {
        return a + b;
}

 

 5.Date日期对象

创建日期对象只有构造函数一种方式,使用new关键字

//创建了一个date对象
var myDate = new Date();

//创建日期对象
var myDate=new Date();
        
//获取一个月中的某一天
console.log(myDate.getDate());

//返回本地时间
console.log(myDate().toLocalString());//2018/5/27 下午10:36:23

 注意:以上getxxx的方法都是对时间的获取,如果要设置时间,使用setxxx,请参考链接:http://www.runoob.com/jsref/jsref-obj-date.html

 

6.Math 内置对象

常用内置对象

 6.1 Math.ceil() 向上取整,'天花板函数'

var x = 1.234;
//天花板函数  表示大于等于 x,并且与它最接近的整数是2
var a = Math.ceil(x);
console.log(a);//2

6.2 Math.floor 向下取整,'地板函数'

= 1.234 X var; 
// less than equal to x, and its closest integer. 1 
var Math.floor = B (X); 
the console.log (B); //. 1

6.3 seeking the maximum and minimum of two numbers

// find the maximum value of the minimum of the two numbers 
the console.log (Math.max (2,5)); //. 5 
the console.log (Math.min (2,5)); // 2

6.4 nonce Math.random ()

var ran = Math.random();
console.log(ran);[0,1)

If you were to take a random number between 100-200, how do?

Memorized formulas: min - a random number between max: min + Math.random () * (max-min)

These are the common method JS objects we introduce, if more want to reference, please refer to the link: http://www.runoob.com/jsref/jsref-tutorial.html

 

Guess you like

Origin www.cnblogs.com/xiaozhiqi/p/10974090.html