js整理2
2015-09-07 22:48
549 查看
字符串
类型var a = "abc"; var b = new String( a ); var c = Object( a ); typeof a; // "string" typeof b; // "object" typeof c; // "object" a instanceof String; // false b instanceof String; // true c instanceof String; // true Object.prototype.toString.call( a ); // "[object String]" Object.prototype.toString.call( b ); // "[object String]" Object.prototype.toString.call( c ); // "[object String]"
字符串化(String)
原始类型会返回其字符形式
Array,Function,Object, Date等会返回其自定义的
toString方法的返回值;其中
Object默认的是
Object.prototype.toString(),即返回
[object Object]
布尔类型
布尔化(Boolean)//除了下面的几种,其他都会转化为true undefined null false +0, -0, and NaN "" //特殊 document.all //注意Boolean并没有调用toString,valueOf转化 Boolean([]) //true;
判断
a || b; // roughly equivalent to: a ? a : b; a && b; // roughly equivalent to: a ? b : a; ........ // good enough (works implicitly): if (a) { // .. } // better (works explicitly): if (!!a) { // .. } // also great (works explicitly): if (Boolean( a )) { // .. }
json字符化
JSON.stringify( "42" ); // ""42"" (a string with a quoted string value in it) JSON.stringify( undefined ); // undefined JSON.stringify( function(){} ); // undefined JSON.stringify( [1,undefined,function(){},4] ); // "[1,null,null,4]" JSON.stringify( { a:2, b:function(){} } ); // "{"a":2}"
对于对象,如果包含不合法的json值或者其他问题,必须定义一个
toJSON方法进行安全的操作;(所有实际上
stringify就是字符串化
toJSON返回的值)
var o = { }; var a = { b: 42, c: o, d: function(){} }; // create a circular reference inside `a` o.e = a; // would throw an error on the circular reference // JSON.stringify( a ); // define a custom JSON value serialization a.toJSON = function() { // only include the `b` property for serialization return { b: this.b }; }; JSON.stringify( a ); // "{"b":42}"
第二参数,用于选择/替换,为
Array或
Function;
var a = { b: 42, c: "42", d: [1,2,3] }; //选择对象属性 JSON.stringify( a, ["b","c"] ); // "{"b":42,"c":"42"}" JSON.stringify( a, function(k,v){ if (k !== "c") return v; } ); // "{"b":42,"d":[1,2,3]}"
第三参数,用于占为,应该为
String
JSON.stringify( a, null, "-----" ); // "{ // -----"b": 42, // -----"c": "42", // -----"d": [ // ----------1, // ----------2, // ----------3 // -----] // }"
特殊字符~
效果相当于-(x + 1);
用于判断
indexOf: 由于没有查找到都返回-1,
~-1 = 0 // false;
无进位去小数
Math.floor(54.5); //54 Math.floor(-54.5); //-55 Math.ceil(54.5); //55 Math.ceil(-54.5); //-54 ~~54.5 //54 ~~-54.5 //-54
特殊字符+
对象和原始类型或对象和对象进行时,会分别调用对象的valueOf, toString方法转化成原始类型后处理
var a = [1,2]; var b = [3,4]; a + b; // "1,23,4" //注意{} {} + []; //0 ({}) + []; //[object Object] // var a = { valueOf: function() { return 42; }, toString: function() { return 4; } }; a + ""; // "42" String( a ); // "4"
注意其他算术符号都是数值化的
'1' - 0; //1 [] * []; //0
比较
不严格相等
数值和字符串判断,字符串转化为数值;任何值和布尔值判断,都转化为数值;
只有
undefined和
null自我或相互判断时才相等
对象和非对象判断,先将对象转化为原生类型(分别调用
valueOf,toString)
"foo" == [ "foo" ]; // true [] == ![]; // true 0 == "\n"; => 0 == " " // true //用于判断undefined和null if (a == null) { //... }
大小比较
数值/布尔值和其他类型比较,都先转化为数值;其他情况都先转化为字符串;true > [] //true // var a = { b: 42 }; var b = { b: 43 }; a < b; // false a > b; // false a == b; // false a <= b; // true => [object Object] == [object Object] a >= b; // true
相关文章推荐
- JS 面向对象随笔
- JSON
- JavaScript之prototype
- js事件知识回顾
- JavaScript之对象的遍历
- Javascript邮箱增删
- javascript输入验证
- 【h5-egret】js加载成第三方库
- 利用js实现简单计算器
- JavaScript学习笔记7-附录(回掉函数)
- Hello JSP!——动作元素基础练习题
- JSP/Servlet-----JSTL之I18N formatting标签库
- 用ArcGIS API for JavaScript在地图上绘制圆多边形
- json解析
- fastJSON
- JSON 解析
- javascript 构造函数内return 解析
- JS获取汉字首字母
- JSon
- Hello JSP!——<jsp:plugin>、<jsp:params>、<jsp:fallback>动作元素篇