js整理1
数组
- 比较时的隐式转化
var a = [1,2,3];
var b = [1,2,3];
a == b; //false
a == '1,2,3'; //true;
//
var c = [];
Boolean(c); //true
c == false; //true
c == 0; //true
c == ''; //true
c == undefined; //false
- 类数组
var arr = Array.prototype.slice.call( arguments );
//es6
var arr = Array.from( arguments );
- 字符串使用数组方法
var a = "foo";
var c = Array.prototype.join.call( a, "-" );
var d = Array.prototype.map.call( a, function(v){
return v.toUpperCase() + ".";
} ).join( "" );
c; // "f-o-o"
d; // "F.O.O."
//
var c = Array.prototype.reverse.call( a ); //error
var c = a.split( "" ).reverse().join( "" );
- 关于undefined
var a = [ undefined, undefined, undefined ]; //[ undefined, undefined, undefined ]
var b = new Array( 3 ); //[ undefined x 3 ]
var c = [];
c.length = 3; //[ undefined x 3 ]
var d = [ , , , ]; //[ undefined x 3 ]
a.join( "-" ); // "--"
b.join( "-" ); // "--"
a.map(function(v,i){ return i; }); // [ 0, 1, 2 ]
b.map(function(v,i){ return i; }); // [ undefined x 3 ]
//确实想要表示多个空值而不是不存在;
var a = Array.apply( null, { length: 4 } );
a; // [ undefined, undefined, undefined ,undefined]
数值
- 浮点数精确度
0.1 + 0.2 === 0.3; // false
//
if (!Number.EPSILON) { //es6
Number.EPSILON = Math.pow(2,-52);
}
function numbersCloseEnoughToEqual(n1,n2) {
return Math.abs( n1 - n2 ) < Number.EPSILON;
}
var a = 0.1 + 0.2;
var b = 0.3;
numbersCloseEnoughToEqual( a, b ); // true
numbersCloseEnoughToEqual( 0.0000001, 0.0000002 ); // false
- 判断正负0
function isNegZero(n) {
n = Number( n );
return (n === 0) && (1 / n === -Infinity);
}
- 数值化(Number)
- 对于原始类型:
false//0, true//1, null//0, 数值字符串//数值, 其他//NaN; - 对于其他类型: 调用其
valueOf, toString,对返回的原始类型进行操作;否则报错;
- 对于原始类型:
Number( "" ); // 0
Number( [] ); // 0
- 小心使用
parseInt, 它也会调用内部的toString方法;
parseInt( 1/0, 19 ); =>parseInt( "Infinity", 19 ) => parseInt( "I", 19 ) // 18;
parseInt( 0.000008 ); // 0 ("0" from "0.000008")
parseInt( 0.0000008 ); // 8 ("8" from "8e-7")
parseInt( false, 16 ); // 250 ("fa" from "false")
parseInt( parseInt, 16 ); // 15 ("f" from "function..")
parseInt( "0x10" ); // 16
parseInt( "103", 2 ); // 2
对象
- 对象引用
//引用被切断
function foo(x) {
x.push( 4 );
x; // [1,2,3,4]
// later
x = [4,5,6]; //改变arg[x]的引用
x.push( 7 );
x; // [4,5,6,7]
}
var a = [1,2,3];
foo( a );
a; // [1,2,3,4] not [4,5,6,7]
//引用一直保存
function foo(x) {
x.push( 4 );
x; // [1,2,3,4]
// later
x.length = 0; // empty existing array in-place
x.push( 4, 5, 6, 7 );
x; // [4,5,6,7]
}
var a = [1,2,3];
foo( a );
a; // [4,5,6,7] not [1,2,3,4]
//注意特殊对象的隐式转化
function foo(x) {
x = x + 1;
x; // 3
}
var a = 2;
var b = new Number( a ); // or equivalently `Object(a)`
foo( b );
console.log( b ); // 2, not 3
void操作符
- 使不返回任何值
//可能用到的情况
function doSomething() {
// note: `APP.ready` is provided by our application
if (!APP.ready) {
// try again later
return void setTimeout( doSomething, 100 );
}
var result;
// do some other stuff
return result;
}
等值判断
//es6
Object.is( a,b );
不用于严格意义的对比,而是用于特殊例子的比较
var a = 2 / "foo";
var b = -3 * 0;
Object.is( a, NaN ); // true
Object.is( b, -0 ); // true
Object.is( b, 0 ); // false
预定义数值
if(VALUE) {
console.log('value');
} //error
if(typeof VALUE !== "undefined") {
console.log('value')
}
//
function () {
var newValue = (typeof oldValue !== "undefined") ? oldValue : 'newvalue';
...
}
function (oldValue) {
var newValue = oldValue || 'newvalue';
...
}
js原生类型
- String()
- Number()
- Boolean()
- Array()
- Object()
- Function()
- RegExp()
- Date()
- Error()
- Symbol() -- added in ES6!
由于所有类型的本源都是来自Object(),使用Object.prototype.toString.call方法可以现在类型内部的[[class]];
格式为[object X]
- 构建时注意
//Object
var a = new Object();
var b = Object();
var c = new Object;
//Array
var a = new Array();
var b = Array();
var c = new Array;
//Date
var a = new Date(); //[object Date];
var b = Date(); //string
var c = new Date; //[object Date];
- 各个类型的原型都是其相对应类型的空值;//注意不要污染它们;
Array.prototype; //[];
......
js整理1的更多相关文章
- Dynamics CRM 日常使用JS整理(二)
BPF(Business Process Flow)相关的JS 为Stage添加changed或者selected事件: function fnOnLoad() { Xrm.Page.data.pro ...
- Dynamics CRM 日常使用JS整理(一)
整理下平时CRM开发中用到的一些基本的js操作 取值: var oResult = Xrm.Page.getAttribute(sFieldName).getValue(); var oResult ...
- js整理
Js脚本语音 网页里面使用的脚本语音 基础语法 注释语法 单行注释// 多行注释/**/ 嵌入js代码 尽量靠下写 用<script type="text/javascript& ...
- Vue.js 整理笔记
以前我们用Jquery进行dom的操作,虽然熟悉后开发效率很高,但是如果多个控件的相互操作多的情况下,还是会乱.相比之下,Vue的使用更加清晰,通过虚拟dom将数据绑定,而且组件化和路由的帮助下,让整 ...
- js整理3
函数 call: fun.call(a), a会转化成相应的对象,函数内的this即指向它; function foo() { console.log(this); } foo.call(null); ...
- node.js整理 07例子
需求 一个简单的静态文件合并服务器,该服务器需要支持类似以下格式的JS或CSS文件合并请求. http://assets.example.com/foo/??bar.js,baz.js 在以上URL中 ...
- node.js整理 06异步编程
回调 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了 function heavyCompute(n, callback) { var count = 0, i, j; for (i = ...
- node.js整理 05进程管理
简介 NodeJS可以感知和控制自身进程的运行环境和状态,也可以创建子进程并与其协同工作,这使得NodeJS可以把多个程序组合在一起共同完成某项工作,并在其中充当胶水和调度器的作用 常用API Pro ...
- node.js整理 03文件操作-遍历目录和文本编码
遍历目录 递归算法 遍历目录时一般使用递归算法,否则就难以编写出简洁的代码. 递归算法与数学归纳法类似,通过不断缩小问题的规模来解决问题 function factorial(n) { if (n = ...
随机推荐
- mysql时间字符串按年/月/天/时分组查询
SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * ) FROM test GROUP BY DATE_FORMAT( ...
- C/S love自编程序
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 【2016-09-16】UbuntuServer14.04或更高版本安装问题记录
出于项目需要,我们的Qt程序需要运行在 1. Windows/Linux-X86平台(CPU为常见的桌面级CPU如G3220.I3等): 2. Windows/Linux-X86低功耗平台(CPU为I ...
- CROSS JOIN连接用于生成两张表的笛卡尔集
将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘 CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个 ...
- 微信支付 - V3支付问题
参考资料:http://www.2cto.com/weixin/201506/407690.html 1.微信公众号支付出错: 当前页面的URL未注册: get_brand_wcpay_reque ...
- EasyUi–8.datebox赋值的问题
这个问题要从EasyUI的datebox组件说起,小菜用这个组件的时候,发现用$("#id").val()这种形式,居然拿不到文本框的值! 经过度娘的帮助,发现可以用$( ...
- wifi基础知识整理
转自 :http://blog.chinaunix.net/uid-9525959-id-3326047.html WIFI基本知识整理 这里对wifi的802.11协议中比较常见的知识做一个基本的总 ...
- 重温WCF之WCF抛出异常的处理SOAP Fault(十二)
1.(服务端)抛出和(客户端)捕获SOAP Fault 当我们需要客户端获取到WCF服务端的抛出的异常的时候,使用FaultException类 WCF类库在System.ServiceModel命名 ...
- 使用python递归子目录处理日志文件
重要说明: (1)python使用4个空格进行层次缩进的(不是tab),在eclipse里面可以直接使用tab缩进,是因为eclipse会实时地将tab转成4个空格 (2)在eclipse中安装pyD ...
- AngularJS讲义 - 作用域
什么是作用域? Angular中作用域(scope)是模板以及工作的上下文环境,作用域中存放了应用模型和视图相关的回调行为.作用域是层次化结构的与相关联的DOM结构相对应.作用域可以观察表达式以及传播 ...