本地对象
2017-06-29 20:03
141 查看
在 ECMAScript 中,所有对象并非同等创建的。一般来说,可以创建并使用的对象有三种:本地对象、内置对象和宿主对象。
Object、Function、Array、String、Number、Date、Boolean、RegExp、Error、EvalError、RangeError、
Number对象
创建Number对象
当 Number() 和运算符 new 一起作为构造函数使用时,它返回一个新创建的 Number 对象。如果不用 new 运算符,把 Number() 作为一个函数来调用,它将把自己的参数转换成一个原始的数值,并且返回这个值(如果转换失败,则返回 NaN)。
Number对象方法
toString()方法 可把一个 Number 对象转换为一个字符串,并返回结果,使用指定的基数。
toLocalString()方法可把一个 Number 对象转换为本地格式的字符串。
toFixed()方法可把 Number 四舍五入为指定小数位数的数字。
toExponentail()方法可把对象的值转换成指数计数法;返回结果的字符串表示,采用指数计数法,即小数点之前有一位数字,小数点之后有 num 位数字。该数字的小数部分将被舍入,必要时用 0 补足,以便它达到指定的长度。
toPrecision() 方法把数字格式化为指定的长度,可在对象的值超出指定位数时将其转换为指数计数法。
valueOf() 方法可以字符串返回数字。
Function对象的方法
Function 对象也有与所有对象共享的 valueOf() 方法和 toString() 方法。这两个方法返回的都是函数的源代码,在调试时尤其有用。
Array()对象的方法
concat()方法连接两个或更多的数组,该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
join() 方法用于把数组中的所有元素放入一个字符串。
pop() 方法用于删除并返回数组的最后一个元素。
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
reverse() 方法用于颠倒数组中元素的顺序。
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
slice() 方法可从已有的数组中返回选定的元素。
sort() 方法用于对数组的元素进行排序。
splice() 方法向/从数组中添加/删除元素,然后返回被删除的元素。
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
String对象方法
charAt() 方法可返回指定位置的字符。
concat() 方法用于连接两个或多个字符串。
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
split() 方法用于把一个字符串分割成字符串数组。
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
substring() 方法用于提取字符串中介于两个指定下标之间的字符。
Math对象方法
ceil() 方法可对一个数进行上舍入。
floor() 方法可对一个数进行下舍入。
max() 方法可返回两个指定的数中带有较大的值的那个数。
min() 方法可返回指定的数字中带有最低值的数字。
random() 方法可返回介于 0 ~ 1 之间的一个随机数。
round() 方法可把一个数字舍入为最接近的整数。
RegExp 对象方法
exec() 方法用于检索字符串中的正则表达式的匹配。
test() 方法用于检测一个字符串是否匹配某个模式.
本地对象
ECMA-262 把本地对象(native object)定义为“独立于宿主环境的 ECMAScript 实现提供的对象”。简单来说,本地对象就是 ECMA-262 定义的类(引用类型)。它们包括:Object、Function、Array、String、Number、Date、Boolean、RegExp、Error、EvalError、RangeError、
Number对象
创建Number对象
var numberObj = new Number(); var numberObj = Number();
当 Number() 和运算符 new 一起作为构造函数使用时,它返回一个新创建的 Number 对象。如果不用 new 运算符,把 Number() 作为一个函数来调用,它将把自己的参数转换成一个原始的数值,并且返回这个值(如果转换失败,则返回 NaN)。
Number对象方法
toString()方法 可把一个 Number 对象转换为一个字符串,并返回结果,使用指定的基数。
var number = new Number(1256); alert(number.toString()); //返回结果1256
toLocalString()方法可把一个 Number 对象转换为本地格式的字符串。
var number = new Number(145); alert(number.toLocaleString()); //返回结果145
toFixed()方法可把 Number 四舍五入为指定小数位数的数字。
var number = new Number(14.587); alert(number.toFixed(1));//指定保留小数一位 //返回结果14.6
toExponentail()方法可把对象的值转换成指数计数法;返回结果的字符串表示,采用指数计数法,即小数点之前有一位数字,小数点之后有 num 位数字。该数字的小数部分将被舍入,必要时用 0 补足,以便它达到指定的长度。
var number = new Number(10000); alert(number.toExponential(1));//规定指数计数法的的小数保留一位 //返回结果1.0e+4 var number = new Number(10000); alert(number.toExponential(2));//规定指数计数法的的小数保留两位 //返回结果1.00e+4
toPrecision() 方法把数字格式化为指定的长度,可在对象的值超出指定位数时将其转换为指数计数法。
var num = new Number(10000); alert(num.toPrecision(3))//规定num的长度为3位数 //返回结果1.00e+4;
valueOf() 方法可以字符串返回数字。
var number = new Number('1423'); alert(number.valueOf());//返回结果1423 alert(typeof(number.valueOf()));//返回number类型
Function对象的方法
Function 对象也有与所有对象共享的 valueOf() 方法和 toString() 方法。这两个方法返回的都是函数的源代码,在调试时尤其有用。
//toString() function doAdd(iNum) { alert(iNum + 10); } document.write(doAdd.toString()); //输出结果function doAdd(iNum) { alert(iNum + 10); } //valueOf() function doAdd(iNum) { alert(iNum + 10); } document.write(doAdd.valueOf()); //输出结果function doAdd(iNum) { alert(iNum + 10); }
Array()对象的方法
concat()方法连接两个或更多的数组,该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
//连接数组中的值 var a = [1,2,3]; document.write(a.concat(4,5,6)); //输出结果1,2,3,4,5,6
//连接两个数组 var arr = new Array(3); arr[0] = "George"; arr[1] = "John"; arr[2] = "Thomas"; var arr2 = new Array(2); arr2[0] = "James"; arr2[1] = "Adrew"; document.write(arr.concat(arr2)); //输出结果George,John,Thomas,James,Adrew
join() 方法用于把数组中的所有元素放入一个字符串。
var arr = new Array(3); arr[0] = "George" arr[ 4000 1] = "John" arr[2] = "Thomas" document.write(arr.join());//默认使用","分隔 输出结果George,John,Thomas document.write(arr.join("."));//指定使用"."分隔,输出结果George.John.Thomas
pop() 方法用于删除并返回数组的最后一个元素。
var arr = new Array(3); arr[0] = "George"; arr[1] = "John"; arr[2] = "Thomas"; document.write(arr);//返回结果George,John,Thomas document.write(arr.pop());//返回结果Thomas document.write(arr);//返回结果George,John
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr) //返回 George,John,Thomas document.write(arr.push("James")) //返回长度4 document.write(arr) //返回 George,John,Thomas,James
reverse() 方法用于颠倒数组中元素的顺序。
var arr = new Array('George','John','Thomas') document.write(arr.reverse()))//返回Thomas,John,George
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
var arr = new Array('George','John','Thomas') document.write(arr.shift())//返回George document.write(arr)//返回John,Thomas
slice() 方法可从已有的数组中返回选定的元素。
var arr = "hello world"; alert(arr.slice(2));//返回llo world 2表示从下标2开始到结束 alert(arr.slice(2,7));//返回llo w 2表示从下标2开始到下标7结束但不包括7
sort() 方法用于对数组的元素进行排序。
var arr = ['2','56','34','8']; console.log(arr.sort());//返回2,34,56,8 var arr = ['d','q','a','u']; console.log(arr.sort());//返回a,d,q,z
splice() 方法向/从数组中添加/删除元素,然后返回被删除的元素。
var arr2 = ['d','q','a','u']; arr2.splice(2,0,'op');//删除从下标2开始的0个元素并添加元素op document.write(arr2);//返回d,q,op,a,u arr2.splice(2,1,'op');//删除从下标2开始的1个元素,并添加元素op代替被删除的元素 document.write(arr2);//返回d,q,op,u
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
var arr2 = ['d','q','a','u']; document.write(arr2.unshift('f'));//返回新数组的长度5 document.write(arr2)//返回f,d,q,a,u
String对象方法
charAt() 方法可返回指定位置的字符。
var str="Hello world!" document.write(str.charAt(1))//1代表下标 //返回e
concat() 方法用于连接两个或多个字符串。
var str1="Hello " var str2="world!" document.write(str1.concat(str2)) //返回结果 Hello world!
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
//查找指定的值 var str="Hello world!" document.write(str.match("world"))//返回world document.write(str.match("World"))//返回null document.write(str.match("worlld"))//返回null document.write(str.match("world!"))//返回world!
//用正则表达式查找字符串 var str="1 plus 2 equal 3" document.write(str.match(/\d+/g))//匹配所有的数字 //返回结果1,2,3
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
var str="hello world" document.write(str.replace(/world/, "mike"))//将world替换为mike,返回结果hello mike
var str="hello world beautiful world" document.write(str.replace(/world/g, "girl"))//g代表全局替换,也就是找到所有的world替换为girl,返回结果hello girl beautiful girl
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
var str="Hello world!" document.write(str.indexOf("Hello"))//返回0,在下标0出现 document.write(str.indexOf("World"))//返回-1,检索不到 document.write(str.indexOf("world"))//返回6,在下标6出现
lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
var str="Hello world!" document.write(str.lastIndexOf("Hello"))//返回0 document.write(str.lastIndexOf("World"))//返回-1 document.write(str.lastIndexOf("world"))//返回6
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
var str="hello World!" document.write(str.search(/World/));//返回6,字符串world在下标6出现 var str2="hello World!" document.write(str2.search(/world/));//返回-1,search()对大小写敏感 var str2="hello World!" document.write(str2.search(/world/i))//返回6,i表示忽略大小写
slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
var str="Hello happy world!" document.write(str.slice(6))//提取从下标6开始的所有字符,返回结果 happy world!
var str="Hello happy world!" document.write(str.slice(6,11))//提取从下标6开始到下标11之间的所有字符,但不包括下标11的字符,返回结果 happy
split() 方法用于把一个字符串分割成字符串数组。
var str="How are you doing today?" document.write(str.split(" "))//将字符串分隔成字符串数组,返回结果 How,are,you,doing,today? document.write(str.split("") + "<br />")//将字符串每个字符之间都会被分隔,返回结果H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? document.write(str.split(" ",3))//将字符串分隔成字符串数组并且数组长度为3
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
var str="Hello world!" document.write(str.substr(3))//提取从下标3开始的字符,返回结果 lo world!
var str="Hello world!" document.write(str.substr(3,7))//提取从下标3开始长度为7的字符。返回结果 lo worl
substring() 方法用于提取字符串中介于两个指定下标之间的字符。
var str="Hello world!" document.write(str.substring(3))//提取从下标3开始的字符,返回结果 lo world!
var str="Hello world!" document.write(str.substring(3,7))//提取从下标3开始到下标7之间的字符,不包括下标7的字符,返回结果 lo w
Math对象方法
ceil() 方法可对一个数进行上舍入。
document.write(Math.ceil(0.60))//返回1,向上取整 document.write(Math.cei bbfc l(0.40))//返回1,向上取整 document.write(Math.ceil(5))//返回5 document.write(Math.ceil(5.1))//返回6,向上取整 document.write(Math.ceil(-5.1))//返回-5,向上取整 document.write(Math.ceil(-5.9))//返回-5,向上取整
floor() 方法可对一个数进行下舍入。
document.write(Math.floor(0.60))//返回0,向下取整 document.write(Math.floor(0.40))//返回0,向下取整 document.write(Math.floor(5))//返回5 document.write(Math.floor(5.1))//返回5,向下取整 document.write(Math.floor(-5.1))//返回-6,向下取整 document.write(Math.floor(-5.9))//返回-6,向下取整
max() 方法可返回两个指定的数中带有较大的值的那个数。
document.write(Math.max(5,7))//返回7 document.write(Math.max(-3,-5))//返回-3
min() 方法可返回指定的数字中带有最低值的数字。
document.write(Math.max(5,7))//返回5 document.write(Math.max(-3,-5))//返回-5
random() 方法可返回介于 0 ~ 1 之间的一个随机数。
document.write(Math.random())//返回0到1之间的随机数,不包括0和1,例如 0.003323571620420074
round() 方法可把一个数字舍入为最接近的整数。
document.write(Math.round(0.60)) //大于0.5,向上舍入,返回结果1 document.write(Math.round(0.50)) //大于等于0.5,向上舍入,返回结果1 document.write(Math.round(0.49))//小于0.5,向下舍入 ,返回结果0 document.write(Math.round(-4.40))//大于-4.50,向上舍入,返回结果-4 document.write(Math.round(-4.60))//小于-4.50,向下舍入,返回结果-5
RegExp 对象方法
var str="Every man in the world! Every woman on earth!"; patt=/man/g; //全局搜索man str2=str.replace(patt,"person");//全局搜索man并用person替换man document.write(str2); //返回结果Every person in the world! Every woperson on earth! patt=/(wo)?man/g;//全局搜索以wo开头紧接着是man的字符串 patt.compile(patt); str2=str.replace(patt,"person"); document.write(str2); //返回结果Every person in the world! Every person on earth!
exec() 方法用于检索字符串中的正则表达式的匹配。
var str = "Visit W3School" var a = new RegExp('W3School','g'); document.write(a.exec(str));//返回结果W3School
test() 方法用于检测一个字符串是否匹配某个模式.
var str = "Visit W3School" var a = new RegExp('W3School'); document.write(a.test(str))//返回true
相关文章推荐
- (有码)OC将对象归档到本地文件
- ajaxpro 对象未定义,在本地调试没问题,但是IIS没效果
- 要将Point对象从显示对象的本地坐标转换成舞台的全局坐标
- hibernate 管理Session:Session 对象的生命周期与本地线程绑定
- Easyui实现datagrid绑定JS本地对象(可不请求URL,不生成JSON)
- 用于持久对象的本地查询 - 克服基于字符API的缺陷(译)
- Qt——web网页和本地对象的交互
- 是否可以修改 JavaScript 的本地对象(native object)
- 44.java编程思想——传递和返回对象 制作本地副本
- 《JavaScript高级程序设计》读书笔记(十):本地对象Date
- 对象序列化到本地文件 ObjectOutputstream ObjcetInputstream
- 类对象序列化(反序列化)成xml文件或者二进制文件到本地文件方法
- js 面向对象学习5 本地对象与内置对象
- Flash本地共享对象 SharedObject
- JavaScript---本地及内置对象
- Android本地最简单的数据存储,没有之一(让SharedPreferences存取JavaBean对象或List<Bean>)
- Session 对象的生命周期与本地线程绑定
- JavaScript中的本地对象、内置对象、宿主对象
- JavaScript常用本地对象小结
- 本地对象Array的原型扩展