您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

js面向对象中内置对象的详解(代码)

2024/7/6 20:32:12发布43次查看
本篇文章给大家带来的内容是关于js面向对象中内置对象的详解(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
方法string获取字符串长度length属性
连接字符串:concat(string, string)
获取索引值:indexof(string)
根据索引值获取单个字符:charat(index)
从起始索引fromindex开始截取长度length的字符串:substr(fromindex,length)
截取 起始索引startindex  到  结束索引endindex的子字符串,结果包含startindex处的字符,不包含endindex处的字符:substring(startindex,endindex)/slice(startindex,endindex)
按给定字符串分割,返回分割后的多个字符串组成的字符串数组:split(string)
使用选择的分隔符将一个数组合并为一个字符串:join(string)
大小写转换:tolowercase()/touppercase()
replace
判断是否包含指定字符串:contains(string)
判断是否为空:isempty()
html编码和解码:escapehtml() unescapehtml()
replace的用法:
//$符的使用mystring=mystring.replace(/(marvin)/g,"<font color=red>$1</font>");//函数function test(str){ return "<font color='red'>"+str+"</font>"}mystring=mystring.replace(/(marvin)/g,test);
array添加和删除:shift unshift【从数组开头开始添加或者删除】,pop push【从数组末尾添加或者删除】
数组合并,参数添加到原数组中,返回新的数组:concat()
排序: sort() 从小到大 reverse() 从大到小
数组截取: slice(start,end)
数组拼接成字符串:join(separator)
splice()
datedate = new date(2015, 2, 27, 12, 59, 59);
- date.tolocalestring(): 2015年3月27日 12:59:59
- date.tolocalestring(): 2015年3月27日 12:59:00
- date.tolocalestring(): 2015年3月27日 12:00:00
- date.tolocalestring(): 2015年3月27日 12:00:00
- date.tolocalestring(): 2015年3月27日 12:00:00
date = new date(“month dd,yyyy hh:mm:ss”):date = new date(‘2014-12-25’)
getfullyear()
getyear()
getmonth()
getdate(): 获取几号 - 0 - 31 比如25
getday(): 获取星期几 - 比如星期3的3
gethours()
getminutes()
getseconds()
getmilliseconds()
gettime(): 获取相对于1970-01-01的毫秒值
functionarguments
length
caller属性 获取调用当前函数的函数。caller属性只有当函数正在执行时才被定义
callee属性 返回正被执行的 function 对象,即指定的 function 对象的正文
constructor 属性 就是用来构造对象实例的函数引用。
prototype属性获取对象的原型。每一个构造函数都有一个prototype属性,指向另一个对象。这个对象的所有属性和方法,都会被构造函数的实例继承。这意味着,我们可以把那些不变的属性和方法,直接定义在prototype对象上。
apply方法 调用函数,并用指定对象替换函数的this值,同时用指定数组替换函数的参数。
call方法 调用一个对象的方法,用另一个对象替换当前对象。
bind方法 对于给定函数,创建具有与原始函数相同的主体的绑定函数。
tostring 返回对象的字符串表示形式。
call::借用,伪数组
//call的用法:借用()var myclass={getallpersonnumbers:function(age){return age}};var student={ getdetail:function(){ return {name:'marvin',age:20} } };myclass.getallpersonnumbers.call(student)//call的用法:借用(传参)var myclass={getallpersonnumbers:function(){return 33}};var student={ getdetail:function(){ return {name:'marvin',age:20} } };myclass.getallpersonnumbers.call(student,20)//call 的用法伪数组(将伪数组改为真 的数组)var json = {1:'',2:'',length:2}
call方法会修改this的指针,在call后改变被借用的函数的this指针指向自身。
//call将指向全局的this的指针指向了自身studentmyclass.getallpersonnumbers.call(student,20)
伪数组:获取到的dom就是一个伪数组,jquery获取到的也是一个伪数组
var ss = {0: 'marvin', 1: 'lili'}array.prototype.slice.call(ss)//能将具有length属性的对象转成数组=> [marvin , lili]
apply::
//循环式获取最大值function getmax(arr){ var arrlen=arr.length; for(var i=0,ret=arr[0];i<arrlen;i++){ ret=math.max(ret,arr[i]); }return ret;}//max()只能传递离散的数值//获取数组中的最大值myclass.getallstudentsnumbers.apply(student,[10,200])return math.max.call(null,1,2,3,4,5) === return math.max.apply(null,arr);//合并数组array.prototype.push.apply(arr1,arr2)
相关推荐:
js中的内置对象array详解
js的内置对象string详解
以上就是js面向对象中内置对象的详解(代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product