Тёмный

Shirmung Bielefeld: == ? === ??? ...#@^% | JSConf EU 

JSConf
Подписаться 277 тыс.
Просмотров 9 тыс.
50% 1

What is type coercion? When can it be useful? What happens when we choose to use == over ===?
JavaScript is a polarizing language, said to be full of “good parts” and “bad parts”. JavaScript’s type coercion is often bemoaned-one of the common “gotchas” for newcomers to the language. In this talk, we’ll unravel the complexity of type coercion and define a set of simple rules to make our lives easier. No more #@^%’s.
Transcript: 2014.jsconf.eu/speakers/shirmu...
License: For reuse of this video under a more permissive license please get in touch with us. The speakers retain the copyright for their performances.
Cover photo by @FotoVerite

Наука

Опубликовано:

 

13 окт 2014

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 7   
@user-xw6jr7ue4b
@user-xw6jr7ue4b Год назад
For 15 minutes it couldn't be more informative, thanks! For anyone who wants to dive deeper into this topic, look out for these: - the method toString is being implemented differently in the prototypes of the built-in function-constructors. That's the reason why dates, arrays, etc. are being converted differently into strings/numbers. The method toString is returning a primitive value (in the built-in objects it's returning a string usually but you can override that to return any primitive type) which is being converted into string/number based on the rules for converting primitive types into strings/numbers that were mentioned clearly in the video. You can easily check that: console.log(Object.prototype.toString === Array.prototype.toString) //false - if a Symbol.toPrimitive property (which value is a function) exists in the objects (including its prototype chain ofc), it's being converted into a string/number based on the rules that are defined in that function. That's the reason why dates are being converted differently into numbers compared to the other built-in objects. You can easily check what: console.log(Date.prototype[Symbol.toPrimitive]) //the property exists console.log(Array.prototype[Symbol.toPrimitive]) //the property doesn't exist. The instances are being converted based on the default rules that I already mentioned
@mangeshsodnar8426
@mangeshsodnar8426 Год назад
Most detailed video on type coercions.....thank you.
@bhavesh2952
@bhavesh2952 Год назад
Very well explained.
@neomashego
@neomashego 4 года назад
Extremely useful thanks :,-)
@PiyushChauhan2011
@PiyushChauhan2011 9 лет назад
Great talk, Shirmung. I'm confused about these statements.. [] == false // empty list compared to false, returns true [] == true // empty list compared to true, returns false if([]) { console.log('True'); } else { console.log('False'); } // returns True :D How can list be both false and true ? :D, don't understand what's going on underneath the hood! Same case with Infinity!
@ZengaTV
@ZengaTV 9 лет назад
I have to admit that this threw me for a loop as well. if ([])... makes sense as all objects are evaluated to true, and arrays in javascript are objects. [] == false on the other hand is less obvious but can be explained by #14-22 if one operand is a boolean, convert it to a number: [] == false => [] == 0 if one of the operands is string or a number, and the other one is an object, convert object to a primitive. Since [] is an object it is converted to primitive. I'm not certain which approach is taken here by the engine, but I'm guessing toString is called as valueOf returns the object itself. Thus: [] == 0 => "" == 0 Last, but not least: If one of the operands is a string and the other is a number, convert string to a number: "" == 0 => 0 == 0 => true
@aiboy88
@aiboy88 9 лет назад
JSConf Yes. I'm sorry. I defently acted like one.
Далее
Coercion in Javascript
14:53
Просмотров 37 тыс.
Lasagna Soup @Lionfield
00:35
Просмотров 6 млн
Mathias Bynens: JavaScript ♥ Unicode
25:42
Просмотров 7 тыс.
Asynchrony: Under the Hood - Shelley Vohr - JSConf EU
25:24
The JS Call Stack Explained In 9 Minutes
9:30
Просмотров 85 тыс.
Сложная распаковка iPhone 15
1:01
Просмотров 14 тыс.
Так ли Хорош Founders Edition RTX 4080 ?
13:00