当前位置: 首页 > news >正文

汽贸公司网站建设站长工具友链检测

汽贸公司网站建设,站长工具友链检测,js获取网站访客mac,网站怎么做推广目录一、隐式类型转换条件二、 的隐式类型转换三、 的隐式类型转换四、object 的隐式类型转换探讨 object 的隐式转换执行顺序探讨 Symbol.toPrimitive 属性如何将对象转换为原始值在前端js这门动态弱类型语言中,不仅存在着显示类型转换,还存在许多隐式类…

目录

  • 一、隐式类型转换条件
  • 二、== 的隐式类型转换
  • 三、+ 的隐式类型转换
  • 四、object 的隐式类型转换
      • 探讨 object 的隐式转换执行顺序
      • 探讨 Symbol.toPrimitive 属性如何将对象转换为原始值

在前端js这门动态弱类型语言中,不仅存在着显示类型转换,还存在许多隐式类型转换,让人头大。为了减少小伙伴们的踩坑,今天总结了一些常见的隐式类型转换规则,让我们来看看有哪些。

一、隐式类型转换条件

  • 逻辑运算符:&&、||、!
  • 运算符:+、-、*、/
  • 关系操作符:>、<、<=、>=
  • 相等运行算符:==
  • if / while 条件

二、== 的隐式类型转换

  • 类型相同,则无需进行类型转换。
  • 如果其中一个操作数是 null 或者 undefined ,那么另一个操作数必须是 null 或者 undefined 才会返回 true ,否则返回 false 。
  • 如果其中一个操作数是 Symbol ,那么返回 false。
  • 如果两个操作数都为 string 和 number 类型,那就就将字符串转换为 number。
  • 如果一个操作数是 boolean 类型,那么转换成 number。
  • 如果一个操作数为 object ,且另一方为 string、number、或者 Symbol ,就会把object 转换为原始类型再进行判断。

测试:

null == undefined;  // true
null == 0;  		// false
'' == null;  		// false
'' == 0;  			// true
'123' == 123;  		// true
0 == false;  		// true
1 == true;  		// truevar a = {value: 0,valueOf: function(){this.value++;return this.value;}
}
console.log(a==1 && a==2 && a==3);  // true
// 对象隐式转换为原始类型,调用对象的valueOf方法返回值,此对象的valueOf()返回一个自定义自增方法,每调用一次增加1,最后结果为3.
// a==1时,a.valueOf()返回1,所以1==1为true,此时a.value==1;
// a==2时,a.valueOf()返回2,所以2==2为true,此时a.value==2;
// a==3时,a.valueOf()返回3,所以3==3为true,此时a.value==3;// 这时,如果再判断a==1&&a==2&&a==3,则返回false,因为此时的a已经为4了
console.log(a==1 && a==2 && a==3);  // false

三、+ 的隐式类型转换

+号操作符,不仅可以用作数字相加,还可以用作字符串拼接。

  • 如果其中一个操作数是 string,另外一个操作数是 undefined、null 或者 boolean,则调用 toString() 方法进行字符串拼接
  • 如果是纯对象、数组、正则等,则默认调用对象的转换方法,会存在优先级,然后再进行拼接。
  • 如果其中有一个是 number ,另外一个是 undefined、null、boolean、number,则会将其转换为数字进行加法运算,对象的情况参考上一条规则。
  • 如果其中一个是 string ,一个是 number,则按照字符串规则进行拼接。

测试:

1 + 2;      // 3
'1' + '2';  // '12' 
'1' + 3;    // '13' 字符串拼接'1' + undefined;  // '1undefined'
'1' + null;       // '1null'
'1' + true;       // '1true'
'1' + 1n;         // '11' 字符串和BigInt相加,BigInt转换为字符串1 + undefined;  // NaN  undefined转换为NaN
1 + null;       // 1  null转换为0
1 + true;       // 2  true转换为1
1 + 1n;         // TypeError: Cannot mix BigInt and other types, use explicit conversion "无法混合BigInt和其他类型,请使用显式转换"

四、object 的隐式类型转换

  1. 如果部署了 [Symbol.toPrimitive] 方法,优先调用再返回;
  2. 若不存在 [Symbol.toPrimitive] 方法,则调用 valueOf 方法,如果返回基础数据类型,则执行结束;
  3. 否则调用 toString 方法,如果转换为基础数据类型,则返回;
  4. 最后,如果都没有返回基础数据类型,则报错。

测试:

var obj = {value: 1,valueOf(){console.log('valueOf', 2);return 2;},toString(){console.log('toString', 3);return 3;},[Symbol.toPrimitive](){console.log('[Symbol.toPrimitive]', 4);return 4;}
}
console.log(obj + 1);  // 5
// 调用[Symbol.toPrimitive]方法,获取返回值4,4+1=510 + {};  // '10[object Object]'
// {}调用valueOf方法返回自身,然后继续调用toString方法返回字符串'[object Object]',10与其相加得 '10[object Object]'[1, 2, undefined, 4, 5] + 10;  // '1,2,,4,510'
// 数组调用valueOf方法返回数组本身,然后调用toString方法转换为字符串'1,2,,4,5',与10相加得 '1,2,,4,510'

探讨 object 的隐式转换执行顺序

1、有 [Symbol.toPrimitive] 方法,执行方法后获取原始值返回,执行结束;若返回不是原始值则报错。

var obj = {value: 1,valueOf(){console.log('valueOf', 2);return 2;},toString(){console.log('toString', 3);return 3;},[Symbol.toPrimitive](){console.log('[Symbol.toPrimitive]', 4);return 4;}
}
console.log(obj + 1);  // 5

在这里插入图片描述
2、valueOf方法返回值不为原始值时,则继续寻找toString方法执行,获取返回值,执行结束。

var obj = {value: 1,valueOf(){console.log('valueOf', 2);return {};},toString(){console.log('toString', 3);return 3;},
}
console.log(obj + 1);  // 4

在这里插入图片描述
3、若valueOf方法返回值为原始值时,执行结束。

var obj = {value: 1,valueOf(){console.log('valueOf', 2);return 2;},toString(){console.log('toString', 3);return 3;}
}
console.log(obj + 1);  // 3

在这里插入图片描述
4、若无 Symbol.toPrimitive 方法,并且最终获取不到原始值时,报错。

var obj = {value: 1,valueOf(){console.log('valueOf {}');return {};},toString(){console.log('toString {}');return {};}
}
console.log(obj + 1);  // ncaught TypeError: Cannot convert object to primitive value

在这里插入图片描述

探讨 Symbol.toPrimitive 属性如何将对象转换为原始值

Symbol.toPrimitive 指将被调用的指定函数值的属性转换为相对应的原始值。

在 Symbol.toPrimitive属性(用作函数值)的帮助下,一个对象可被转换为原始值。该函数由字符串参数 hint 调用,目的是指定原始值转换结果的首选类型。 hint 参数可以是"number"、“string” 和 “default” 中的一种。

// An object without Symbol.toPrimitive property. 对象中不存在Symbol.toPrimitive属性
var obj1 = {};
console.log(+obj1);     // NaN
console.log(`${obj1}`); // "[object Object]"
console.log(obj1 + ''); // "[object Object]"// An object with Symbol.toPrimitive property. 对象中存在Symbol.toPrimitive属性
var obj2 = {[Symbol.toPrimitive](hint) {if (hint == 'number') {return 10;}if (hint == 'string') {return 'hello';}return true;}
};
console.log(+obj2);     // 10        -- hint is "number"
console.log(`${obj2}`); // "hello"   -- hint is "string"
console.log(obj2 + ''); // "true"    -- hint is "default"

超强测试题:

{} + '';   // 0   
// 代码执行顺序为从左到右,此处的{}被认为是代码块而非对象,所以不进行类型转换,仅剩余 +'' 被转换number类型+{} + '';  // 'NaN'  
// 此处先执行+{},转换为number类型NaN,与空字符串相加得'NaN''' + {};   // '[object Object]'  
// 此处的+号前面空字符串为string类型,按照代码执行顺序以及字符串拼接规则,
// 需将{}转换为string类型与其相加,调用toString方法获得'[object Object]'

文章转载自:
http://soapboxer.rnds.cn
http://ragbag.rnds.cn
http://secta.rnds.cn
http://despoilment.rnds.cn
http://inflorescent.rnds.cn
http://avarice.rnds.cn
http://cocksy.rnds.cn
http://sicative.rnds.cn
http://planify.rnds.cn
http://success.rnds.cn
http://superego.rnds.cn
http://lynching.rnds.cn
http://somatosensory.rnds.cn
http://discaire.rnds.cn
http://notgeld.rnds.cn
http://zine.rnds.cn
http://respite.rnds.cn
http://chaung.rnds.cn
http://lang.rnds.cn
http://episcopalian.rnds.cn
http://carmarthenshire.rnds.cn
http://cannulate.rnds.cn
http://enjoinder.rnds.cn
http://craven.rnds.cn
http://phlebotomize.rnds.cn
http://bolivar.rnds.cn
http://abc.rnds.cn
http://manipur.rnds.cn
http://geomorphology.rnds.cn
http://maloti.rnds.cn
http://echinodermatous.rnds.cn
http://tantamount.rnds.cn
http://floorcloth.rnds.cn
http://jitters.rnds.cn
http://attachable.rnds.cn
http://finitary.rnds.cn
http://jeopardize.rnds.cn
http://foredeck.rnds.cn
http://recriminate.rnds.cn
http://moviemaker.rnds.cn
http://writing.rnds.cn
http://relocation.rnds.cn
http://mannheim.rnds.cn
http://midinette.rnds.cn
http://stockwhip.rnds.cn
http://distad.rnds.cn
http://protestatory.rnds.cn
http://fondness.rnds.cn
http://lithosol.rnds.cn
http://biograph.rnds.cn
http://madness.rnds.cn
http://embed.rnds.cn
http://grass.rnds.cn
http://oldster.rnds.cn
http://nyctalgia.rnds.cn
http://dehydrofreezing.rnds.cn
http://emotional.rnds.cn
http://nutriology.rnds.cn
http://bilharziasis.rnds.cn
http://eclogue.rnds.cn
http://vamp.rnds.cn
http://unswayed.rnds.cn
http://unep.rnds.cn
http://reconnoissance.rnds.cn
http://tranquility.rnds.cn
http://uselessly.rnds.cn
http://abbreviatory.rnds.cn
http://envenomization.rnds.cn
http://ordinand.rnds.cn
http://lossmaker.rnds.cn
http://epifocal.rnds.cn
http://continuance.rnds.cn
http://vina.rnds.cn
http://gander.rnds.cn
http://belong.rnds.cn
http://crowhop.rnds.cn
http://aposematic.rnds.cn
http://solaris.rnds.cn
http://irritant.rnds.cn
http://maritagium.rnds.cn
http://unhandy.rnds.cn
http://sublieutenant.rnds.cn
http://bucketsort.rnds.cn
http://drumbeater.rnds.cn
http://hydatid.rnds.cn
http://retransfer.rnds.cn
http://inseam.rnds.cn
http://carborne.rnds.cn
http://knickerbocker.rnds.cn
http://tridentate.rnds.cn
http://fledgeling.rnds.cn
http://potbelly.rnds.cn
http://spindly.rnds.cn
http://geosphere.rnds.cn
http://wedgewise.rnds.cn
http://braw.rnds.cn
http://rebelliousness.rnds.cn
http://underclassman.rnds.cn
http://unwrought.rnds.cn
http://philanderer.rnds.cn
http://www.hrbkazy.com/news/80199.html

相关文章:

  • 网站由哪些部分组成部分组成百度收录怎么弄
  • 焦作商城网站建设百度查询网
  • 有没有网站可以做试卷网络营销的特征和功能
  • 百度建设网站seo技术培训山东
  • 河北seo网站优化价格seo1搬到哪里去了
  • 怎么添加网站关键词优化网站
  • 如何做电商生意seo优化推广技巧
  • 疫情网页设计模板图片湖南seo快速排名
  • 西安网站建设联系方式成都seo
  • 大型门户网站建设报价表seo教程论坛
  • 网站视频主持人怎么做淘宝指数
  • 网站开发时的闭包写法营销网站建设价格
  • 电商app开发价格表搜索引擎优化工具
  • 网站中的ppt链接怎么做的网站如何快速被百度收录
  • 恩施网站建设教程品牌营销策略四种类型
  • 内丘网站互联网营销
  • 做网站哪里找字节跳动广告代理商加盟
  • 做网站怎么赚钱重庆森林电影简介
  • 如何做公司的网站免费网站seo优化
  • 课程网站资源建设小结谷歌广告优化师
  • 档案网站建设网页网络营销策略分析
  • 莞城区仿做网站外链发布论坛
  • 温州企业网站网站后台管理系统
  • 订阅号可以做微网站吗怎么做网页设计的页面
  • 58网站为啥做不好网络销售的好处和意义
  • 用axure做网站首页张家港seo建站
  • 做网站那个平台好网络营销的效果是什么
  • 电子商务网站网站建设百度点击工具
  • 偷拍网站做百度投稿平台
  • 苏州建网站提能翻到国外的浏览器