Home  >  Article  >  Web Front-end  >  Parsing of built-in objects in js

Parsing of built-in objects in js

不言
不言Original
2018-07-14 15:15:571508browse

This article mainly introduces the analysis of the built-in objects of js, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. Data type

Data types in js are divided into two types, original data and reference data types.

1. Original data type

Undefined、Null、Boolean、Number、String是js中五种原始数据类型(primitive type)。

2. Reference data type

引用类型通常叫作类(class),凡是以new创建出来的对象都是引用数据类型。包括new Boolean、new Number等原始类。

3. Original value and reference value

原始值是存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置。
引用值时存储在堆(heap)中的对象,也就是说,存储在变量处的值是一个指针(point),指向存储对象的内存处。
为变量赋值时,ECMAScript的解释程序必须判断该值是原始类型的还是引用类型的,从而把它们放到内存区域。
var a = 100;   // 栈内存
var b = new Number(100);  // 堆内存
console.log(a);  // 数值
console.log(b);  // 对象引用
console.log(b.valueOf());  // 获取数字对象b的值

Special reminder is that in In js, strings are regarded as primitive types, which means that the following operations are very memory intensive.

2. Local objects (built-in objects)

ECMA-262把本地对象(native object)定义为“独立于宿主换将的ECMAScript实现提供的对象”。本地对象就是ECMA-262定义的类(引用类型)。包括:  
- Object Funciton  
- Array String Boolean Number Date RegExp  
- Error EvalError RangeError ReferenceError SyntaxError TypeError URIError

1.Array object

1.Creation syntax

var arr = [element0, element2, ...];
new Array();
new Array(size);
new Array(element0, element1, ...);
var arr = [100, 200, [100, 200], true, undefined, function() { 
console.log("wtf.");
}, {name: "孙悟空", age: 18}];

2.Attributes

constructor  返回对创建此对象的数组函数的引用。
length       设置或返回数组中元素的数目。
prototype    可以向对象添加属性和方法。

3.Method

http://www.w3school.com.cn/jsref/jsref_obj_array.asppush()        向数组的末尾添加一个或更多元素,并返回新的长度。               
unshift()     向数组的开头添加一个或更多元素,并返回新的长度。                
pop()         删除并返回数组的最后一个元素。                               
shift()       删除并返回数组的第一个元素。                               
splice()      删除元素,并向数组添加新元素。                     
slice()       从某个已有的数组返回选定的元素。                    
sort()        对数组的元素进行排序。                           
reverse()     颠倒数组中元素的顺序。
concat()      连接两个或更多的数组,并返回结果。
join()        把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。
toSource()    返回该对象的源代码。
toString()    把数组转换为字符串,并返回结果。
toLocaleString()    把数组转换为本地数组,并返回结果。
valueOf()     返回数组对象的原始值

4.Array traversal

var arr = [1, 3, 5, 7, 9, 11];
for(var i in arr){
    console.log(arr[i]);
}
for(var j=0; j<arr.length; j++){
    console.log(arr[j]);
}
for(var k=arr.length-1; k>=0; k--){
    console.log(arr[k]);
}
function Person(name, force) {
    this.name = name;
    this.force = force;
}
var perArr = [
    new Person("乔峰", 93),
    new Person("段誉", 87),
    new Person("虚竹", 89),
    new Person("扫地僧", 97),
    new Person("慕容博", 88),
    new Person("慕容复", 85),
    new Person("萧远山", 90),
];
var forceArr = [];
for (var i in perArr){
    if (perArr[i].force >= 90){
        forceArr.push(perArr[i]);
    }
}
console.log(forceArr);

2.String object

1.Create

new String(s);
String(s);
// 字符串在底层是以字符数组的形式存储的,这源于js对字符串奇葩的设置:它是原始类型而不是引用类型。在其它大多数语言中,字符串都是引用类型。

2.Properties

constructor  对创建该对象的函数的引用
length       字符串的长度
prototype    允许您向对象添加属性和方法

3.Method

http://www.w3school.com.cn/jsref/jsref_obj_string.asp
concat()         连接字符串。
split()          把字符串分割为字符串数组。
slice()          提取字符串的片断,并在新的字符串中返回被提取的部分。
indexOf()        检索字符串。
lastIndexOf()    从后向前搜索字符串。
substr()         从起始索引号提取字符串中指定数目的字符。
substring()      提取字符串中两个指定的索引号之间的字符。
charAt()         返回在指定位置的字符。
charCodeAt()     返回在指定的位置的字符的 Unicode 编码。
fromCharCode()   从字符编码创建一个字符串。
match()          找到一个或多个正则表达式的匹配。
replace()        替换与正则表达式匹配的子串。
search()         检索与正则表达式相匹配的值。
toSource()     代表对象的源代码。
toString()       返回字符串。
valueOf()        返回某个字符串对象的原始值。
localeCompare()  用本地特定的顺序来比较两个字符串。
anchor()         创建 HTML 锚。
toLocaleLowerCase()   把字符串转换为小写。
toLocaleUpperCase()   把字符串转换为大写。
toLowerCase()    把字符串转换为小写。
toUpperCase()    把字符串转换为大写。
big()           使用大号字体显示字符串。
small()          使用小字号来显示字符串。
bold()           使用粗体显示字符串。
italics()        使用斜体显示字符串。
sup()            把字符串显示为上标。
sub()            把字符串显示为下标。
strike()         使用删除线来显示字符串
link()          将字符串显示为链接。
blink()         显示闪动字符串。
fontcolor()     使用指定的颜色来显示字符串。
fontsize()      使用指定的尺寸来显示字符串。
fixed()         以打字机文本显示字符串。

3.RegExp

1.Create

new RegExp(pattern, attributes);
/pattern/attributes;
参数 pattern 是一个字符串,指定了正则表达式的模式或其他正则表达式。
参数 attributes 是一个可选的字符串,包含属性 "g"、"i" 和 "m",分别用于指定全局匹配、区分大小写的匹配和多行匹配。

2.Attribute

global         RegExp 对象是否具有标志 g。     
ignoreCase     RegExp 对象是否具有标志 i。 
lastIndex      一个整数,标示开始下一次匹配的字符位置。   
multiline      RegExp 对象是否具有标志 m。     
source         正则表达式的源文本。

3.Method

http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
compile       编译正则表达式。
exec          检索字符串中指定的值。返回找到的值,并确定其位置。
test          检索字符串中指定的值。返回 true 或 false。

 4. Methods of String objects that support regular expressions

search        检索与正则表达式相匹配的值。     
match         找到一个或多个正则表达式的匹配。 
replace       替换与正则表达式匹配的子串。     
split         把字符串分割为字符串数组。
关于match
  如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。
  如果 regexp 具有标志 g,则 match() 方法将执行全局检索,找到 stringObject 中的所有匹配子字符串。若没有找到任何匹配的子串,则返回 null。
  match会将匹配到的结果返回到一个数组对象中,不论是一次或是多次匹配。
  在全局检索模式下,match() 即不提供与子表达式匹配的文本的信息,也不声明每个匹配子串的位置。如果您需要这些全局检索的信息,可以使用 RegExp.exec()
var str = "1234Like123Array789language";
result = str.match(/[a-zA-z]+/gi);
console.log(result instanceof Array); //true
console.log(result); // [ &#39;Like&#39;, &#39;Array&#39;, &#39;language&#39; ]
var str = "1234Like123Array789language";
var match = str.match(/[a-zA-z]+/gi);
console.log(match);
var reg = new RegExp("(?<word>[a-zA-z]+)", "g");
var result;
var arr = [];
while ((result = reg.exec(str)) !== null){
    // console.log(result);
    // console.log(result instanceof Array);
    // console.log(reg.lastIndex);
    arr.push(result);
}
console.log(arr);

4.Date

1.Create

var myDate=new Date();

2.Attributes

constructor  返回对创建此对象的 Date 函数的引用。
prototype    可以向对象添加属性和方法。

 3.Method


http://www.w3school.com.cn/jsref/jsref_obj_date.asp
Date()               返回当日的日期和时间。
getDate()            从 Date 对象返回一个月中的某一天 (1 ~ 31)。
etDay()              从 Date 对象返回一周中的某一天 (0 ~ 6)。
getMonth()           从 Date 对象返回月份 (0 ~ 11)。
getFullYear()        从 Date 对象以四位数字返回年份。
getHours()           返回 Date 对象的小时 (0 ~ 23)。
getMinutes()         返回 Date 对象的分钟 (0 ~ 59)。
getSeconds()         返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds()    返回 Date 对象的毫秒(0 ~ 999)。
setDate()            设置 Date 对象中月的某一天 (1 ~ 31)。
setMonth()           设置 Date 对象中月份 (0 ~ 11)。
setFullYear()        设置 Date 对象中的年份(四位数字)。
setHours()           设置 Date 对象中的小时 (0 ~ 23)。
setMinutes()         设置 Date 对象中的分钟 (0 ~ 59)。
setSeconds()         设置 Date 对象中的秒钟 (0 ~ 59)。
setMilliseconds()    设置 Date 对象中的毫秒 (0 ~ 999)。
对象中的秒钟 (0 ~ 59)。// setUTCMilliseconds()    根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。// toSource()  返回该对象的源代码。// toString()  把 Date 对象转换为字符串。// toTimeString()  把 Date 对象的时间部分转换为字符串。// toDateString()  把 Date 对象的日期部分转换为字符串。// toGMTString()   请使用 toUTCString() 方法代替。// toUTCString()   根据世界时,把 Date 对象转换为字符串。// toLocaleString()    根据本地时间格式,把 Date 对象转换为字符串。// toLocaleTimeString()    根据本地时间格式,把 Date 对象的时间部分转换为字符串。// toLocaleDateString()    根据本地时间格式,把 Date 对象的日期部分转换为字符串。// UTC()   根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。// valueOf()   返回 Date 对象的原始值。对象中的秒钟 (0 ~ 59)。// setUTCMilliseconds()    根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。// toSource()  返回该对象的源代码。// toString()  把 Date 对象转换为字符串。// toTimeString()  把 Date 对象的时间部分转换为字符串。// toDateString()  把 Date 对象的日期部分转换为字符串。// toGMTString()   请使用 toUTCString() 方法代替。// toUTCString()   根据世界时,把 Date 对象转换为字符串。// toLocaleString()    根据本地时间格式,把 Date 对象转换为字符串。// toLocaleTimeString()    根据本地时间格式,把 Date 对象的时间部分转换为字符串。// toLocaleDateString()    根据本地时间格式,把 Date 对象的日期部分转换为字符串。// UTC()   根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。// valueOf()   返回 Date 对象的原始值。
getTime()            返回 1970 年 1 月 1 日至今的毫秒数。
parse()              返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
getTimezoneOffset()  返回本地时间与格林威治标准时间 (GMT) 的分钟差。
getUTCDate()         根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
getUTCDay()          根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
getUTCMonth()        根据世界时从 Date 对象返回月份 (0 ~ 11)。
getUTCFullYear()     根据世界时从 Date 对象返回四位数的年份。
getUTCHours()        根据世界时返回 Date 对象的小时 (0 ~ 23)。
getUTCMinutes()      根据世界时返回 Date 对象的分钟 (0 ~ 59)。
getUTCSeconds()      根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
setTime()            以毫秒设置 Date 对象。
setUTCDate()         根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCMonth()        根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCFullYear()     根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours()        根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMinutes()      根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCSeconds()      根据世界时设置 Date

5.Math

1.Create

直接使用Math关键字即可
Math.属性
Math.方法([参数1, ...])

2.Attribute

E           返回算术常量 e,即自然对数的底数(约等于2.718)。
LN2         返回 2 的自然对数(约等于0.693)。
LN10        返回 10 的自然对数(约等于2.302)。
LOG2E       返回以 2 为底的 e 的对数(约等于 1.414)。
LOG10E      返回以 10 为底的 e 的对数(约等于0.434)。
PI          返回圆周率(约等于3.14159)。
SQRT1_2     返回返回 2 的平方根的倒数(约等于 0.707)。
SQRT2       返回 2 的平方根(约等于 1.414)。

3.Method

http://www.w3school.com.cn/jsref/jsref_obj_math.asp
abs(x)      返回数的绝对值。
acos(x)     返回数的反余弦值。
asin(x)     返回数的反正弦值。
atan(x)     以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x)  返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
ceil(x)     对数进行上舍入。
cos(x)      返回数的余弦。
exp(x)      返回 e 的指数。
floor(x)    对数进行下舍入。
log(x)      返回数的自然对数(底为e)。
max(x,y)    返回 x 和 y 中的最高值。
min(x,y)    返回 x 和 y 中的最低值。
pow(x,y)    返回 x 的 y 次幂。
random()    返回 0 ~ 1 之间的随机数。
round(x)    把数四舍五入为最接近的整数。
sin(x)      返回数的正弦。
sqrt(x)     返回数的平方根。
tan(x)      返回角的正切。
toSource()  返回该对象的源代码。
valueOf()   返回 Math 对象的原始值。

6.Number

1.Create

var myNum=new Number(value);
var myNum=Number(value);

 2.Properties

MAX_VALUE   可表示的最大的数。
MIN_VALUE   可表示的最小的数。
NaN         非数字值。
NEGATIVE_INFINITY   负无穷大,溢出时返回该值。
POSITIVE_INFINITY   正无穷大,溢出时返回该值。
constructor 返回对创建此对象的 Number 函数的引用。
prototype   使您有能力向对象添加属性和方法。ue);

 3.Methods

http://www.w3school.com.cn/jsref/jsref_obj_number.asp
toString        把数字转换为字符串,使用指定的基数。
toLocaleString  把数字转换为字符串,使用本地数字格式顺序。
toFixed         把数字转换为字符串,结果的小数点后有指定位数的数字。
toExponential   把对象的值转换为指数计数法。
toPrecision     把数字格式化为指定的长度。
valueOf         返回一个 Number 对象的基本数字值。

 7.FunctionsGlobal functions

http://www.w3school.com.cn/jsref/jsref_obj_global.asp
decodeURI()             解码某个编码的 URI。
decodeURIComponent()    解码一个编码的 URI 组件。
encodeURI()             把字符串编码为 URI。
encodeURIComponent()    把字符串编码为 URI 组件。
escape()                对字符串进行编码。
unescape()              对由 escape() 编码的字符串进行解码。
eval()                  计算 JavaScript 字符串,并把它作为脚本代码来执行。
getClass()              返回一个 JavaObject 的 JavaClass。
isFinite()              检查某个值是否为有穷大的数。
isNaN()                 检查某个值是否是数字。
Number()                把对象的值转换为数字。
parseFloat()            解析一个字符串并返回一个浮点数。
parseInt()              解析一个字符串并返回一个整数。
String()                把对象的值转换为字符串。

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of custom objects in js

Analysis of event models in js

The above is the detailed content of Parsing of built-in objects in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn