js整理2

字符串

  • 类型
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}"

  • 第二参数,用于选择/替换,为ArrayFunction;
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

比较

不严格相等

  • 数值和字符串判断,字符串转化为数值;
  • 任何值和布尔值判断,都转化为数值;
  • 只有undefinednull自我或相互判断时才相等
  • 对象和非对象判断,先将对象转化为原生类型(分别调用valueOf,toString)
"foo" == [ "foo" ];       // true
[] == ![];      // true
0 == "
";    => 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
原文地址:https://www.cnblogs.com/jinkspeng/p/4790190.html