javascript常见方法
来源:互联网 发布:云计算架构师待遇 编辑:程序博客网 时间:2024/05/17 06:46
JavaScript
数值
Js代码
/**
* 转换&取整
*/
-45.67890^0 //-45
-45.67890|0 //-45
~~5645.1132 //5645
'-45.67890'^0 //-45
'-45.67890'|0 //-45
~~'5645.1132' //5645
/**
*金额处理
*/
Number.prototype.fixMoney=function(){
return this.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,"$1,")
}
var a=123456789
alert(a.fixMoney())
/**
* 转换&取整
*/
-45.67890^0 //-45
-45.67890|0 //-45
~~5645.1132 //5645
'-45.67890'^0 //-45
'-45.67890'|0 //-45
~~'5645.1132' //5645
/**
*金额处理
*/
Number.prototype.fixMoney=function(){
return this.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,"$1,")
}
var a=123456789
alert(a.fixMoney())
数组
Js代码
var a = [1,2,3];
var b = [4,5,6];
Array.prototype.push.apply(a, b);
eval(a); //[1,2,3,4,5,6]
/**
*指定位置合并
*/
var a = [1,2,3,7,8,9];
var b = [4,5,6];
a.splice.apply(a, Array.concat(3, 0, b));
/**
*最大值最小值
*/
Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1
var a = [1,2,3];
var b = [4,5,6];
Array.prototype.push.apply(a, b);
eval(a); //[1,2,3,4,5,6]
/**
*指定位置合并
*/
var a = [1,2,3,7,8,9];
var b = [4,5,6];
a.splice.apply(a, Array.concat(3, 0, b));
/**
*最大值最小值
*/
Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1
浏览器事件
Js代码
/**
*派发(增加)事件
*/
function appendEvent(dom,event,fun){
if (-[1, ]) { //非ie
dom.addEventListener(event, fun, false);
} else {
window.parent.window.attachEvent("on"+event, fun)
}
}
/**
*取消默认事件
*/
e=e||window.event;
e.preventDefault?e.preventDefault():e.returnValue=false;
/**
*派发(增加)事件
*/
function appendEvent(dom,event,fun){
if (-[1, ]) { //非ie
dom.addEventListener(event, fun, false);
} else {
window.parent.window.attachEvent("on"+event, fun)
}
}
/**
*取消默认事件
*/
e=e||window.event;
e.preventDefault?e.preventDefault():e.returnValue=false;
继承
Js代码
var a=function(v1){
this.v1=v1
this.test=function(){
alert(this.v1)
}
}
var b=function(){
}
b.prototype=new a('12312')
var b1=new b('tttt')
b1.test()
function classA(t){
this.t=t
this.sayArg=function(){
alert(this.t)
}
}
function classB(tt){
this.extend=classA
this.extend(tt)
delete this.extend
}
var b2=new classB('test')
b2.sayArg()
function classC(cc){
this.c=cc
this.sayC=function(){
alert(this.c)
}
}
function classD(cc){
/ classC.call(this,cc)
classC.apply(this,[cc])
}
var d =new classD('dddddd')
d.sayC()
var a=function(v1){
this.v1=v1
this.test=function(){
alert(this.v1)
}
}
var b=function(){
}
b.prototype=new a('12312')
var b1=new b('tttt')
b1.test()
function classA(t){
this.t=t
this.sayArg=function(){
alert(this.t)
}
}
function classB(tt){
this.extend=classA
this.extend(tt)
delete this.extend
}
var b2=new classB('test')
b2.sayArg()
function classC(cc){
this.c=cc
this.sayC=function(){
alert(this.c)
}
}
function classD(cc){
// classC.call(this,cc)
classC.apply(this,[cc])
}
var d =new classD('dddddd')
d.sayC()
其他
Js代码
/**
*随机数
*/
Math.random().toString(16).substring(2); //14位
Math.random().toString(36).substring(2); //11位
/**
*赋值处理
*/
a= [b, b=a][0];//交换值
var a = b && 1;
//相当于
if (b) {
a = 1
}
var a = b || 1; //适合用法方法参数的默认值
//相当于
if (b) {
a = b;
} else {
a = 1;
}
new Array(50).join("a,")//相当于ruby,python中的"a,"*50
var date = +new Date; //转为日期的数值
//获取文件自身的位置
var path=document.scripts;
path=path[path.length-1].src.substring(0,path[path.length-1].src.lastIndexOf("/")+1);
if((path.indexOf('http')!=0&&path.indexOf('/')!=0)||path.indexOf('./')==0){
path=window.location.href.substring(0,window.location.href.lastIndexOf("/")+1)+path
}
- javascript常见方法
- javascript 常见方法
- JavaScript常见调试方法
- JavaScript常见字符串方法
- JavaScript常见事件处理方法
- JavaScript中常见的方法
- 常见的排序方法(javascript)
- javascript常见日期格式化方法
- [JavaScript] document的常见属性和方法
- 最常见的JavaScript DOM方法一览表
- javascript常见的数据校验方法
- JavaScript 数组去重的常见方法!
- [js点滴]JavaScript字符串常见方法总结
- 12种Javascript解决常见浏览器兼容问题的方法
- 12种Javascript解决常见浏览器兼容问题的方法
- ASP.NET调用javascript脚本的常见方法小结
- javascript(JS)常见的表单输入限制方法代码
- javascript(JS)常见的表单输入限制方法代码
- 硬盘安装Linux图文教程大全
- 旺旺图标生成
- 个人开公司的流程
- 铣削
- java实现调用打印机代码详解
- javascript常见方法
- Zend Studio
- tomcat nio
- tomcat插件地址
- jspSmartUpload上传文件
- 主题:Maven实战(一)安装和配置
- ssh
- 在线办公
- dsideal_yy