// 比较
[] == ![] // true
NaN !== NaN // true
1 == true // true
2 == true // false
"2" == true // flase
null > 0 // false
null < 0 // false
null == 0 // false
null >= 0 // true
// 加法
true + 1 // 1
undefined + 1 // NaN
let obj = {};
{} + 1 // 1,这里的 {} 被当成了代码块
{ 1 + 1 } + 1 // 1
obj + 1 // [object Object]1
{} + {} // Chrome 上显示 "[object Object][object Object]",Firefox 显示 NaN
[] + {} // [object Object]
[] + a // [object Object]
+ [] // 等价于 + "" => 0
{} + [] // 0
a + [] // [object Object]
[2,3] + [1,2] // '2,31,2'
[2] + 1 // '21'
[2] + (-1) // "2-1"
// 减法或其他操作,无法进行字符串连接,因此在错误的字符串格式下返回 NaN
[2] - 1 // 1
[2,3] - 1 // NaN
{} - 1 // -1
// String
let value = true;
console.log(typeof value); // boolean
value = String(value); // now value is a string "true"
console.log(typeof value); // string
// Number
let str = "123";
console.log(typeof str); // string
let num = Number(str); // becomes a number 123
console.log(typeof num); // number
let age = Number("an arbitrary string instead of a number");
console.log(age); // NaN, conversion failed
// Boolean
console.log( Boolean(1) ); // true
console.log( Boolean(0) ); // false
console.log( Boolean("hello") ); // true
console.log( Boolean("") ); // false
| 原始值 | 转化为数值类型 | 转化为字符串类型 | 转化为 Boolean 类型 |
|---|---|---|---|
| false | 0 | "false" | false |
| true | 1 | "true" | true |
| 0 | 0 | "0" | false |
| 1 | 1 | "1" | true |
| "0" | 0 | "0" | true |
| "1" | 1 | "1" | true |
| NaN | NaN | "NaN" | false |
| Infinity | Infinity | "Infinity" | true |
| -Infinity | -Infinity | "-Infinity" | true |
| "" | 0 | "" | false |
| "20" | 20 | "20" | true |
| "twenty" | NaN | "twenty" | true |
| [ ] | 0 | "" | true |
| [20] | 20 | "20" | true |
| [10,20] | NaN | "10,20" | true |
| ["twenty"] | NaN | "twenty" | true |
| ["ten","twenty"] | NaN | "ten,twenty" | true |
| function(){} | NaN | "function(){}" | true |
| { } | NaN | "[object Object]" | true |
| null | 0 | "null" | false |
| undefined | NaN | "undefined" | false |
ToPrimitive(input, PreferredType?)
var ToPrimitive = function(obj,preferredType){
var APIs = {
typeOf: function(obj){
return Object.prototype.toString.call(obj).slice(8,-1);
},
isPrimitive: function(obj){
var _this = this,
types = ['Null','Undefined','String','Boolean','Number'];
return types.indexOf(_this.typeOf(obj)) !== -1;
}
};
// 如果 obj 本身已经是原始对象,则直接返回
if(APIs.isPrimitive(obj)) {return obj;}
// 对于 Date 类型,会优先使用其 toString 方法;否则优先使用 valueOf 方法
preferredType = (preferredType === 'String' || APIs.typeOf(obj) === 'Date' ) ? 'String' : 'Number';
if(preferredType==='Number'){
if(APIs.isPrimitive(obj.valueOf())) { return obj.valueOf()};
if(APIs.isPrimitive(obj.toString())) { return obj.toString()};
}else{
if(APIs.isPrimitive(obj.toString())) { return obj.toString()};
if(APIs.isPrimitive(obj.valueOf())) { return obj.valueOf()};
}
throw new TypeError('TypeError');
}
let obj = {
valueOf:() => {
return 0;
}
}
obj + 1 // 1
obj = {
valueOf: function () {
console.log("valueOf");
return {}; // not a primitive
},
toString: function () {
console.log("toString");
return {}; // not a primitive
}
}
obj + 1
// error
Uncaught TypeError: Cannot convert object to primitive value
at <anonymous>:1:5
obj[Symbol.toPrimitive] = function(hint) {
// return a primitive value
// hint = one of "string", "number", "default"
}
user = {
name: "John",
money: 1000,
[Symbol.toPrimitive](hint) {
console.log(`hint: ${hint}`);
return hint == "string" ? `{name: "${this.name}"}` : this.money;
}
};
// conversions demo:
console.log(user); // hint: string -> {name: "John"}
console.log(+user); // hint: number -> 1000
console.log(user + 500); // hint: default -> 1500
prim1 := ToPrimitive(value1) prim2 := ToPrimitive(value2)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有