未加星标

+ operation on JavaScript objects

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In javascript, there are two types of values: primitive and object . Primitives consist undefined , null , boolean , number and string . Other values such as array and function are objects. When applying + operation on different type of values, there would be three kinds of type conversion.

Primitive conversion Number conversion String conversion

There three type conversions have correspondingabstract operationsin JavaScript: ToPrimitive(), ToNumber(), ToString().

For number addition, it's normal math addition(ToNumber). For strings, they are just string concatenations(ToString). For objects, it's a bit different because ToPrimitive doesn't know whether it's a number or string. Let's first take a look at below two expressions, what's the value of each expression?

({} + {}) ({} + [])

When adding the objects, each object will be converted to its corresponding primitive value with ToPrimitive () and addition onthe primitive values will be performed. The flow of addition would be:

Evaluate the left-hand side, and get the value. Evaluate the right-hand side, and get the value. CallToPrimitiveon both the left-hand and right-hand sides(No hint) If either primitive value is aString, skip to 7. CallToNumberon both values. Return the sum of the values. CallToStringon both values. Return the concatenation of both values.

There are three hints: number , string and default . Normally if no hint is specified while doing the primitive conversion, number will be used as the hint. When the hint is number or default, it will try to call valueOf() first then toString() to get the primitive value. For a normal object(Except for Date, RegExp etc), the valueOf() will returns the object itself, hence the object is still not a primitive value, it will call toString() to get the primitive value. Hence an Object will finally call toString() to get the primitive value.

In above cases, {} will be converted to String which has value [object Object], an empty array will be converted to an empty string.

Hence the above two expressions have below results:

[object Object][object Object] [object Object]

This behavior is different from other dynamic languages where + operation usually means merge. For example, in Ruby, if adding two arrays, it will return a new array which contains elements from both arrays.

irb(main):001:0> a = [1, 2] => [1, 2] irb(main):002:0> b = [3, 4] => [3, 4] irb(main):003:0> a + b => [1, 2, 3, 4]

While in JavaScript, the two arrays will be converted to strings first. Each element in each array will also be converted to string first and delimited by comma. The same expression above in Ruby will have below result in JavaScript.

a = [1, 2] (2) [1, 2] b = [3, 4] (2) [3, 4] a + b "1,23,4"

The purpose of this post is to emphasize that basics are really important to understand something. Even though you may know well about AngularJS, jQuery, React or VueJS, you may not be considered as a good front end developer if you don't have a good understanding of Vanilla JavaScript.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

tags: will,value,object,primitive,values,gt,JavaScript,both,converted
分页:12
转载请注明
本文标题:+ operation on JavaScript objects
本站链接:https://www.codesec.net/view/604934.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(10)