JS 基础
来源:互联网 发布:tensorflow安装 32位 编辑:程序博客网 时间:2024/06/01 07:50
1.基础类型与对象
基础:数字,字符串,布尔,null , undefined
对象:数组(有序的),日期(Date),正则(RegExp)
2.浮点数计算 相等问题
贴代码:
var a =3.2;
var b = 2.2;
var c= 1.2;
//(a-b).toFixed(2)==(b-c).toFixed(2);
var val = a+c-2*b;
if(Math.abs(a+c-2*b)<0.001){
alert("相等");
}else{
alert("不相等");
}
提供了两种方法。
3.Infinity 与 NaN
非0除以0=Infinity
0/0 等于NaN 之所以没有用== 因为NaN!=NaN
(突然觉得祖先发明了数字来计数,到现在科学家研究的数字,数论。。但到了0就变成了一个黑洞,仿佛是一个理论无法解释的漏洞,0与1差别的很大,但减法却和2-1一样,题外话)
4.日期时间
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
};
console.log(new Date().format("时间为:yyyy-MM-dd hh:mm"));
调用的时候注意顺序;
5.字符串
"\xA9" 版权符号
“\u03c0” π符号
常用的方法:
s.lenth;
s.charAt(0):第一个字符
s.substring(1,4) 第二个到第四个字符 abcde->bcd
s.slice(1,4) 同上
s.slice(-3) 最后三个字符
s.indexOf('a'); O 是大写
s.split(','); 分割字符串
s.replace('h','H') 全文字符替换
s.toUpperCase() 大写相对应该还有 toLowCase();
6.布尔
经常看到 if(data&&data.length>0)
这里的 if(data) 其实是判断的data 不是false,不为null 或者undefined的不严格判断
具体应该 if(data!=null)
7.
null == undefined ;
typeof null is object but type of undefined is undefined
传空参最佳选择是 null
8.
in 运算符
左操作符是一个字符串或者可以转化为字符串,右边是一个对象。
var data=[7,8,9];
"0" in data ->true
“0”相当于是数组的属性值
- js基础
- js基础
- js基础
- js基础
- JS基础
- js基础
- js基础
- JS基础
- js基础
- js基础
- js基础
- js基础
- JS基础
- js 基础
- js-基础
- js基础
- js基础
- js基础
- leetcode 238. Product of Array Except Self
- Oopencv学习之反向投影
- C#中WinForm实现控件随窗体大小自适应改变
- 没门课程成绩最高分 姓名统计
- AOP
- JS 基础
- 验证码生成组件--JCaptcha的使用
- 装饰者模式
- @Autowired的使用:推荐对构造函数进行注释
- 如何自定义容器网络?- 每天5分钟玩转 Docker 容器技术(33)
- Linux设备驱动模型之platform总线深入浅出(加入设备树)
- 模拟数据库中分批次读取序列号
- jquery中如何在$()中传入形参
- Tomcat端口改为80后访问页面出错